Ait + insert ---->get/set
CTRL + H
在Java中,所有的类都默认直接或者间接继承object类
私有的东西无法被继承
调用父类构造器,必须要在子类构造器的第一行
super注意点
-
super调用父类的构造方法,必须在构造方法的第一个
-
super必须只能出现在子类的方法或者构造方法中!!!
-
super和this不能同时调用构造方法
VS this
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的引用
前提
this:没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法
this();本类的构造
super();父类的构造
方法重写
需要有继承关系,子类重写父类的方法。
-
方法名必须相同
-
参数列表必须相同
-
修饰符:范围可以扩大但不能缩小 public>protected>default>pravate
-
抛出异常:范围可以被缩小,但不能扩大。ClassNotFoundException---->Exception(大)
重写,子类的方法和父类必须一致,方法体不同。
为什么需要重写:
-
父类的功能,子类不一定需要,或者不一定满足!
**ALT+insert;override;
-
public class ht { public static void main(String[] args) { //方法的调用只和左边,定义的数据类型有关 //静态方法和非静态方法不一样 //非静态才可以重写 A a = new A(); a.test(); //父类的引用指向了子类 B b = new A();//子类重写了父类的方法 b.test(); }
多态注意事项
-
多态是方法的多态,属性没有多态
-
父类和子类,有联系 类型转换异常!ClassCastException!
-
存在的条件,继承关系,方法需要重写,父类引用指向子类对象!father f1 = new son();
-
static 方法,属于类,不属于实例
-
final 常量
-
private
public class cs { //一个对象的实际类型是确定的 //可以指向的引用类型就不确定了:父类的引用指向子类 public static void main(String[] args) { //student能调用的方法都是自己的或者继承父类的 student s1 = new student(); //person 父类型,可以指向子类,但不能调用子类独有的方法 person s2 = new student(); Object s3 = new student(); s2.run();//子类重写了父类的方法,执行子类的方法 s1.run(); s1.eat(); //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大 }
类型转换
public class cs { public static void main(String[] args) { Object object = new student(); //System.out.println(X instanceof Y);//能不能编译通过!父子类型 //Object>person>stduent System.out.println(object instanceof student); System.out.println(object instanceof person); System.out.println(object instanceof Object);//true System.out.println(object instanceof String);//false System.out.println(object instanceof teacher);//false person person = new student(); System.out.println(person instanceof student);//true System.out.println(person instanceof person);//true System.out.println(person instanceof Object);//true // System.out.println(person instanceof String);//编译出错 System.out.println(person instanceof teacher);//false student student = new student(); System.out.println(student instanceof student);//true System.out.println(student instanceof person);//true System.out.println(student instanceof Object);//true //System.out.println(person instanceof String);//编译出错 // System.out.println(student instanceof teacher);//false } }
System.out.println(X instanceof Y);//能不能编译通过!父子类型
-
父类引用指向子类的对象
-
把子类转化成父类,向上转型。
-
把父类转化成子类,向下转型。需要强制转换
-
方便方法的调用,减少重复的代码!
抽象 封装 继承 多态
抽象类
abstract
extends 单继承
接口可以进行多继承--插座
抽象就是为了提高开发效率
接口
只有规范,自己无法写方法 专业的约束 约束和实现分离:面向接口编程
接口的本质是契约
package oop.bitter05; //interface接口关键字,接口都需要要有实现类 public interface UserServise { //接口中的所有定义都是抽象的 public abstract void add(String name); void delete(String name); void update(String name); void query(String name); } package oop.bitter05; //类可以实现接口 implements 接口 // 实现了接口中的类,就需要重写接口中的方法 public class UserServiseImpl implements UserServise { @Override public void add(String name) { } @Override public void delete(String name) { } @Override public void update(String name) { } @Override public void query(String name) { } }