黑马程序员技术交流社区

标题: Interface [打印本页]

作者: L番茄X    时间: 2015-5-24 23:25
标题: Interface
//学生跟教师.
abstract class Person
{
   //定义私有的成员属性
    private String name;
    private  int  age ;
   //给个无参的构造方法
    public Person(){
   }
    public Person(String name , int age){
      this.name = name;
      this.age = age;
}
      piublic 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 void sleep(){
    System.out.println("睡觉咯");
}
    //重写吃东西
   public abstract void eat();
}
//接口.
interface IB
{
    void study();
}
interface IA
{
     void teach();
}
class Student extends Person implements IB
  {
//重写成员方法
    public void eat(){
     System.out.println("学生吃饭,喝饮料");
  }
//重写接口的成员方法
    public void study(){
     System.out.println("学生爱学习,天天向上");
  }
class Teacher extends Person implements IA
{
    public void eat(){
    System.out.println("老师吃饭,喜欢喝酒");
  }
    public void teach(){
    System.out.println("老师讲课");
  }
class Demo
{
  public static void main(String[] args)
{
     Person p = new Student();
      p.sleep();
      p.eat();
     IB i = new Student();
     i.study();
   Person k = new Teacher();
    k.sleep();
    k.eat();
   IA a = new Teacher();
    a.teach();

作者: yihuihua    时间: 2015-5-24 23:45
不太明白,接口引用指向子类对象是啥用处,还有代码中好像还可以优化一下
作者: wangkai    时间: 2015-5-25 08:24
Person类中的genName();方法中的public写错了,多了一个i。
作者: 志行    时间: 2015-5-25 09:53
.........大哥  你这哪里和论坛体温有关了,下次换个入学交流的标题好吗~~~~写着论坛体温 然后里面一个总结类型的 程序~~~~
作者: L番茄X    时间: 2015-5-25 12:45
wangkai 发表于 2015-5-25 08:24
Person类中的genName();方法中的public写错了,多了一个i。

谢谢你.




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2