欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java,

来源: javaer 分享于  点击 43721 次 点评:76

java,


第六章 面向对象的三大基石之一   多态

抽象类:

1、有些类不应该被实例化!

2、幸好Java提供了一种方法能够防止类被实例化,换句话说,就是让这个类的对象不能被“new”出来。

3、声明抽象类很简单——在class关键字前面加上抽象类关键字abstract就OK了:

抽象方法:

1、 方法标记为abstract

2、 抽象的类代表此类必须要被extends过

3、抽象的方法代表此方法必须被子类重写。抽象的方法没有方法体:

多态的3个必要条件

1、 要有继承

2、 要有重写

3、父类引用指向子类对象

多态案例

一、Animal父类

public abstract class Animal {

        protected String name;

 

              publicString getName() {

                     returnname;

              }

             

              publicvoid setName(String name) {

                     this.name= name;

              }

              //构造方法

           public Animal(String name){

                  this.name=name;

           }

           public abstract void eat();

}

二、创建动物的子类猫

public class Cat extends Animal{

      

       //构造方法

   public Cat(String name){

           super(name);

    }

   public void eat(){

           System.out.println("一只可爱的小猫,它叫"+this.name+",它正在吃鱼");

    }

}

三、创建动物的子类狗

public class Dog extends Animal{

      

              //构造方法

           public Dog(String name){

                  super(name);

           }

           public void eat(){

                  System.out.println("一只淘气的小狗,它叫"+this.name+",它正在吃骨头");

           }

}

四、创建学生类

public class Student {

  private String name;

 

       publicString getName() {

              returnname;

       }

      

       publicvoid setName(String name) {

              this.name= name;

       }

      

       publicStudent(String name){

              this.name=name;

       }

      

       publicvoid feed(Animal animal){

              animal.eat();

       }

  

}

五、创建测试类

public class Test {

 

       publicstatic void main(String[] args) {

              Catxiaomao=new Cat("汤姆");

              Studentxiaoxin=new Student("小新");

              System.out.print("我叫"+xiaoxin.getName()+",我在喂");

              xiaoxin.feed(xiaomao);

              xiaoxin.feed(newDog("小汪"));

       }

 

}

第七章 面向对象的三大基石之一   接口

 

接口的概念:特殊的抽象类,接口里的方法都是抽象方法。

接口只能实现不能继承,使用关键字implements来实现某个接口。

实现某个接口就是要实现(重写)接口中所有的抽象方法。

一个类既可以继承某个类,也可以实现某几个接口,注意必须先继承后实现。

 

 

接口的应用案例

1、定义父类Telphone为抽象类,添加抽象方法call()和message();

public abstract class Telphone {

    public abstract void call();

    public abstract void message();

}

2、  定义子类CellPhone,实现call()和message();

public class CellPhone extends Telphone{

   //打电话的方法

       @Override

       publicvoid call() {

              System.out.println("大哥大正在用键盘打电话");

       }

   //发短息的方法

       @Override

       publicvoid message() {

              System.out.println("大哥大正在用键盘发短信");

       }

}

3、定义子类SmartPhone,实现call()和message();

public class SmartPhone extends Telphoneimplements IGame {

   //玩游戏的方法

       @Override

       publicvoid playgame() {

              System.out.println("智能机正在玩王者荣耀");

       }

   //打电话的方法

       @Override

       publicvoid call() {

              System.out.println("智能机正在用语音打电话");

       }

   //发短信的方法

       @Override

       publicvoid message() {

              System.out.println("智能机正在用语音发短信");

       }

}

4、定义接口IplayGame(),添加抽象方法playGame();

public interface IGame {      

 public void playgame();

}

5、  SmartPhone继承了Telphone,添加实现IplayGame接口,实现playGame()方法

继承了Telphone:Extends Telphone

实现接口:implements IGame

6、定义学生类Student,添加学生玩游戏的方法,play(IplayGame play){},注意形参为接口类型,只要实现了该接口的对象都可以作为参数传入

public class Student {

   //玩手机

       publicvoid play(IGame i){

          i.playgame();

       }

}

7、  创建测试类,创建学生对象xiaoxin,调用playGame方法时分别传入PSP对象和SmartPhone对象,输出相关内容。

public class TestPhone {

       publicstatic void main(String[] args) {

              CellPhonedgd=new CellPhone();

              dgd.call();

              dgd.message();

             

              SmartPhoneapple=new SmartPhone();

              apple.call();

              apple.message();

              apple.playgame();

             

              Psppsp=new Psp();

              psp.playgame();

             

              System.out.println();

       Student xiaoxin=new Student();

       xiaoxin.play(psp);

       xiaoxin.play(apple);

       }

 

}

 

相关文章

    暂无相关文章
相关栏目:

用户点评