内容:抽象、接口、多态
#########################################################################################################
1、抽象 Abstract
方法抽象了,那么类也得抽象
抽象类不能通过new对象
只有子类覆盖了所有的抽象方法后,子类才能通过new创建对象,如果没有覆盖所有的抽象方法,那么子类还是一个抽象类
抽象关键字abstract和final,private,static不能共存
class ChouXiang
{
public static void main(String[] args)
{
Programmer p = new Programmer("jiang","ji357",7600);
Manager m =new Manager("x","g009",12000,60000);
p.work();
m.work();
p.show();
m.show();
}
}
abstract class Employee
{
private String name;
private String id;
private double salary;
Employee(String name,String id,double salary)
{
this.name = name;
this.id = id;
this.salary = salary;
}
abstract void work();
void show()
{
System.out.println("name:"+name+", id:"+id+", salary:"+salary);
}
}
class Programmer extends Employee
{
Programmer(String name,String id,double salary)
{
super(name,id,salary);
}
void work()
{
System.out.println("code");
}
}
class Manager extends Employee
{
private double bonus;
Manager(String name,String id,double salary,double bonus)
{
super(name,id,salary);
this.bonus = bonus;
}
void work()
{
System.out.println("manage");
}
}eg1###########################################################################################################
2、接口interface
接口的成员有两种:1、全局变量,2、抽象方法
interface Inter{
public static final int PI = 3.141592653589793 ;
public abstract void show1();
public abstract void show2();
}
class De implements Inter{
public void show1(){}
public void show2(){}class JieKouD
{
public static void main(String[] args)
{
SubInter s = new SubInter();
s.show1();
s.show2();
}
}
interface InterA
{
public abstract void show1();
}
interface InterB
{
public abstract void show2();
}
class SubInter implements InterA,InterB
{
public void show1()
{
System.out.println("show1");
}
public void show2()
{
System.out.println("show2");
}
}View Code############################################################################################################
3、多态
多态:也就是继承过后,类型的问题,比如有 Zi类 和 Fu类, Fu f = new Zi(); 这种那么 f 对象不能使用Zi类的特有方法,当通过强制转换过成Zi类型就可以使用子类的方法了。
类用户描述事物的共性基本功能
接口用于定义事物的额外功能