インタフェースと抽象クラスの典型的な応用例
2385 ワード
/*
,
:
: , , ,
: , , ,
, , 。
:
,
();
(){}
, , ,
。
:
:
:
:
*/
//
interface Smoking {
//
public abstract void smoke();
}
//
abstract class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
public void sleep() {
System.out.println(" ");
}
}
//
class Teacher extends Person {
public Teacher() {
}
public Teacher(String name, int age) {
super(name, age);
}
public void eat() {
System.out.println(" ");
}
}
//
class Student extends Person {
public Student() {
}
public Student(String name, int age) {
super(name, age);
}
public void eat() {
System.out.println(" ");
}
}
//
class SmokingTeacher extends Teacher implements Smoking {
public SmokingTeacher() {
}
public SmokingTeacher(String name, int age) {
super(name, age);
}
public void smoke() {
System.out.println(" ");
}
}
//
class SmokingStudent extends Student implements Smoking {
public SmokingStudent() {
}
public SmokingStudent(String name, int age) {
super(name, age);
}
public void smoke() {
System.out.println(" ");
}
}
public class InterfaceDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//
SmokingStudent ss = new SmokingStudent();
ss.setName(" ");
ss.setAge(27);
System.out.println(ss.getName() + "---" + ss.getAge());
ss.eat();
ss.sleep();
ss.smoke();
System.out.println("-------------------");
SmokingStudent ss2 = new SmokingStudent(" ", 30);
System.out.println(ss2.getName() + "---" + ss2.getAge());
ss2.eat();
ss2.sleep();
ss2.smoke();
}
}
/*
* : ---27 ------------------- ---30
*/
実行結果:
---27
-------------------
---30