java学习日志(5)super final,继承封装多态,多态的内存机制 抽象类 接口

时间:2022-12-26 21:54:31

super是直接父类对象的引用

组合可以替代继承


final加在变量前面,则变为常量;夹在方法前面,则不能被重写;加在类前面,则不能被继承。


封装:get, set, 以及boolean变量的get为is


private同一个类可见,default同一个包可见,protected子类可见,public所有类可见。


多态(polymorphism),是方法多态,多态存在的三个必要条件:要有集成,方法的重写,父类的引用指向子类对象。

instanceof


多态内存机制


抽象类(abstract)

定义了抽象类才能定义抽象方法,抽象方法必须被子类实现。抽象类不能被实例化,抽象类只能被继承。


接口(interface):接口的意义是设计和实现的分离。接口里面只有常量和抽象方法

package cn.bjsxt.oop.testInterface;
//测试接口class49,接口的意义是设计和实现的分离
public interface MyInterface {
//接口里面只有常量和抽象方法
/*public static final,接口中常量定义时,写或不写都是这样*/String MAX_GREAD = "boss";
int MAX_SPEED = 120;
/*public abstract写不写都有*/ void test01();
public int test01(int a,int b);
}


package cn.bjsxt.oop.testInterface;
public class Test {
public static void main(String[] args){
Flyable f = new Stone();
f.fly();//只认识前面的几个,不认识weight
}
}