类与对象

类与对象

三大特性
1.封装
2.继承
3.多态

多态
多态是方法的多态,同一个方法根据发送对象的不同而采用不同的行为方式
父类 Father :say(“father”);
子类 Son:say(“son”);eat(“son eat”);

Son son = new Son();
Father father = new Son();
son.say(); //输出son
father.say;//输出son
父类的引用指向子类,若子类重写了父类的方法,则引用会走子类的方法。

father.eat();//出错,父类中没有定义这个方法,故不能使用

抽象类
具有abstract修饰符的类
public abstract class A{
public abstract void do();//只有约束,无实现

}

//继承了抽象类A的,那么子类Son必须实现A中的抽象方法
public class Son extends A{

}

tips:
1.抽象类不能new,只能被继承
2.抽象类中可以有普通方法
3.抽象方法必须在抽象类中

接口
public interface UserService{
int a=0;
void say();
void get();
}

public class UserServiceImpl implements UserService{
@Override
public void say(){

}
}

tips:
1.约束
2.定义方法让不同人实现
3.接口不能实例化,无构造方法
4.多继承
5.重写方法