抽象类:
定义:包含抽象方法的类称为抽象类。
语法:抽象类和抽象方法都必须用abstract声明
抽象类可声明但不可直接实例化,抽象类的实例化必须依靠子类继承来完成。
抽象类中可包含构造方法,但依靠子类调用。
代码示例:
abstract class A{
public A(){ // 构造方法
System.out.println("*************************") ;
}
public abstract void fun() ;
};
class B extends A{ // 继承抽象类
public B(){
super() ;
System.out.println("=========================") ;
}
public void fun(){ // 覆写方法时需要注意访问控制权限
System.out.println("Hello World!!!") ;
}
};
public class AbstractDemo04{
public static void main(String args[]){
B b = new B() ;
}
};
接口:
由抽象方法和全局常量组成,
语法:interface声明,implements实现
interface A{
// public static final String INFO = "CHINA" ;
String INFO = "CHINA" ; // 全局常量
// public abstract void print() ;
void print() ; // 抽象方法
public void fun() ; // 抽象方法
}
class B implements A{ // 子类B实现了接口A
public void print(){ // 实现抽象方法
System.out.println("HELLO WORLD!!!") ;
}
public void fun(){
System.out.println(INFO); // 输出全局常量
}
};
public class InterfaceDemo02{
public static void main(String args[]){
B b = new B() ;
b.print() ;
b.fun() ;
}
};
一个类可以同时实现多个接口,
代码示例:
interface A{
// public static final String INFO = "CHINA" ;
String INFO = "CHINA" ; // 全局常量
// public abstract void print() ;
void print() ; // 抽象方法
public void fun() ; // 抽象方法
}
interface C{
public void funA() ;
}
class B implements A,C{ // 子类B实现了接口A
public void print(){ // 实现抽象方法
System.out.println("HELLO WORLD!!!") ;
}
public void fun(){
System.out.println(INFO); // 输出全局常量
}
public void funA(){
System.out.println("信息:" + INFO) ;
}
};
public class InterfaceDemo03{
public static void main(String args[]){
B b = new B() ;
b.print() ;
b.fun() ;
b.funA() ;
}
};
当一个类既要继承抽象类又要实现接口时
interface A{
public void printA() ;
}
interface B{
public void printB() ;
}
abstract class C{
public abstract void printC() ;
};
class X extends C implements A,B{
public void printA(){}
public void printB(){}
public void printC(){}
};
public class InterfaceDemo04{
public static void main(String args[]){
B b = new B() ;
b.print() ;
b.fun() ;
b.funA() ;
}
};
注意:一个抽象类可以实现多个接口,但接口却不能继承任何抽象类,此外,接口也可以继承接口。
抽象类应用实例:
设计两类人,一类是学生,另一类是工人,他们有着共同属性年龄和姓名,但学生有成绩,工人有薪水。这两类人有相同的行为say,但内容不同,这就需要子类覆写父类的抽象方法
代码:
abstract class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
public void say(){
System.out.println(this.getContent()) ;
}
public abstract String getContent() ;
};
class Student extends Person{
private float score ;
public Student(String name,int age,float score){
super(name,age) ;
this.score = score ;
}
public String getContent(){
return "学生说 --> 姓名:" + super.getName()
+ ";年龄:" + super.getName()
+ ";成绩:" + this.score ;
}
};
class Worker extends Person{
private float salary ;
public Worker(String name,int age,float salary){
super(name,age) ;
this.salary = salary ;
}
public String getContent(){
return "工人说 --> 姓名:" + super.getName()
+ ";年龄:" + super.getName()
+ ";工资:" + this.salary ;
}
};
public class AbstPolDemo02{
public static void main(String args[]){
Person per1 = new Student("张三",20,89.0f) ;
Person per2 = new Worker("李四",30,1189.0f) ;
per1.say() ;
per2.say() ;
}
};
接口应用实例:
接口作为一种标准存在,例如USB接口
interface USB{
public void start() ; // 开始工作
public void stop() ; // 结束工作
}
class Computer{
public static void plugin(USB usb){
usb.start() ;
usb.stop() ;
}
};
class Flash implements USB{
public void start(){
System.out.println("U盘开始工作。") ;
}
public void stop(){
System.out.println("U盘停止工作。") ;
}
};
class Print implements USB{
public void start(){
System.out.println("打印机开始工作。") ;
}
public void stop(){
System.out.println("打印机停止工作。") ;
}
};
public class InterPolDemo02{
public static void main(String args[]){
Computer.plugin(new Flash()) ;
Computer.plugin(new Print()) ;
}
};