1.抽象类的应用---定义模板---WEB开发abstract class Person{ private int age; private String name; 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 abstract void say();
}
class Student extends Person{ int score; public Student(String name,int age,int score){ super(name,age); this.score=score; } public int getScore(){ return this.score; }
public void say(){ System.out.println("名字: "+super.getName()+"年龄: "+super.getAge()+"分数:"+this.getScore()); }}class Worker extends Person{ int salary; public Worker(String name,int age,int salary){ super(name,age); this.salary=salary; } public int getSalary(){ return this.salary; }
public void say(){ System.out.println("名字: "+super.getName()+"年龄: "+super.getAge()+"薪水:"+this.getSalary()); }}
public class NewDemo{ public static void main(String [] args){ Person per1=new Student("海峰",20,40); Person per2=new Worker("邵阳",34,40000);
per1.say(); per2.say();
}}运行的结果为:
2.接口类的应用---定义标准interface USB{ void start(); void stop();}
class Computer{ public static void plugin(USB b){ b.start(); System.out.println("-------USB 设备工作"); b.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 NewDemo{ public static void main(String [] args){ Computer.plugin(new Flash());// 相当于 我只是要USB这个标准 只要你符合这个标准我都可以用 Computer.plugin(new Print()); }}
运行的结果为:
|
|