目录
eg1:
public static void main(String[] args) {
final int MAX_VALUE = 200;
MAX_VALUE = 100;
}
第二句报错:
The final local variable MAX_VALUE cannot be assigned. It must be blank and not using a compound assignment
final修饰的常量不能再更改
eg2:
public class Animal {
public final void run(){
System.out.println("跑");
}
}
class Bird extends Animal{
public void run(){
super.run();
System.out.println("飞");
}
}
子类run方法报错:
Multiple markers at this line
- overrides demo0710.testFinal.Animal.run
- Cannot override the final method from
final修饰的方法不能被子类重写
eg3:
public final class Animal {
public void run(){
System.out.println("跑");
}
}
class Bird extends Animal{
public void run(){
super.run();
System.out.println("飞");
}
}
第二个类报错:
The type Bird cannot subclass the final class Animal
final修饰的类不能被继承
使用访问控制符,实现封装
封装要点
同一个类 | 同一个包中 | 子类 | 所有类 | |
private | ✔ | |||
default(什么都不写) | ✔ | ✔ | ||
protected | ✔ | ✔ | ✔ | |
public | ✔ | ✔ | ✔ | ✔ |
eg private:
Test01.java
public class Test01 {
private String str;
private void print(){
String s = str;
System.out.println("Test01.print()");
}
}
Test02.java
public class Test02 {
public static void main(String[] args) {
Test01 t = new Test01();
t.print();
}
}
Test01调用str正常,Test02调用print时报错:
The method print() from the type Test01 is not visible
eg default:
Test01.java
public class Test01 {
private String str;
void print(){
String s = str;
System.out.println("Test01.print()");
}
}
Test02.java
public class Test02 {
public static void main(String[] args) {
Test01 t = new Test01();
t.print();
}
}
都运行正常
Test03.java(在另一个文件夹)
public class Test03 {
public static void main(String[] args) {
Test01 t = new Test01();
t.print();
}
}
调用print时报错:
The method print() from the type Test01 is not visible
eg protected:
Test01.java
public class Test01 {
private String str;
protected void print(){
String s = str;
System.out.println("Test01.print()");
}
}
Test03.java(在另一个文件夹)
public class Test03 {
public static void main(String[] args) {
Test01 t = new Test01();
t.print();
}
}
Test04.java(在另一个文件夹)
public class Test04 extends Test01 {
public void ttt(){
super.print();
print();
}
public static void main(String[] args) {
Test04 t = new Test04();
t.print();
}
}
3会报错,4运行正常
提供相应的get/set方法来访问相关属性eg:
public class Man {
private String name;
private String id;
public static int cc;
public static final int MAX_SPEED = 120;
}
快速生成get/set方法:
右键——Source——Generate Getters and Setters
可根据需要添加
eg:
定义类:
public class Animal {
String str;
public void voice(){
System.out.println("普通动物叫声");
}
}
class Cat extends Animal {
public void voice(){
System.out.println("喵喵喵");
}
}
class Dog extends Animal {
public void voice(){
System.out.println("汪汪汪");
}
}
class Pig extends Animal {
public void voice(){
System.out.println("哼哼哼");
}
}
测试方法,简化前:
public class Test {
public static void testAnimalVoice(Cat c){
c.voice();
}
public static void testAnimalVoice(Dog c){
c.voice();
}
public static void testAnimalVoice(Pig c){
c.voice();
}
public static void main(String[] args) {
Cat c = new Cat();
testAnimalVoice(c);
}
}
简化后:
public class Test {
public static void testAnimalVoice(Animal c){
c.voice();
}
/*public static void testAnimalVoice(Dog c){
c.voice();
}
public static void testAnimalVoice(Pig c){
c.voice();
}*/
public static void main(String[] args) {
Cat c = new Cat();
testAnimalVoice(c);
}
}
代码:
Animal.java
public class Animal {
String str;
public void voice(){
System.out.println("普通动物叫声");
}
}
class Cat extends Animal {
public void voice(){
System.out.println("喵喵喵");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
Test.java
public class Test {
public static void testAnimalVoice(Animal c){
c.voice();
if(c instanceof Cat){
((Cat) c).catchMouse();
}
}
public static void main(String[] args) {
Animal a = new Cat();
Cat a2 = (Cat)a;
testAnimalVoice(a);
}
}
eg:
public abstract class Animal {
String str;
public abstract void run();
public void breath(){
System.out.println("呼吸");
}
}
class Cat extends Animal{
@Override
public void run() {
System.out.println("猫步小跑");
}
}
class Dog extends Animal{
@Override
public void run() {
System.out.println("狗跑");
}
}
有抽象方法它的类必须是抽象类,此例适用于需要全部重写并且每个子类都要定义的情况。定义抽象类和抽象方法后,该抽象类的子类必须重写抽象类中的抽象方法
本文地址:https://blog.csdn.net/qiao39gs/article/details/107266004
如对本文有疑问, 点击进行留言回复!!
Android 4.0使用Kotlin调用C语言以及汇编语言
Java Class.forName()用法和newInstance()方法原理解析
网友评论