资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
3、类的高级特性(抽象类、接口等)引入:本章继续深入学习面向对象思想。正课:1、抽象类并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息 来描绘一个具体的对象,这样的类就是抽象类。用abstract修饰。定义抽象类:abstract class类名称成员变量;方法();/定义一般方法abstract方法();定义抽象方法范例 testabstract :package testabstract;public abstract class Ani malpublic Ani mal(Stri ng aType)type = new Strin g(aType);public Stri ng toStri ng()return This is a + type;/ Dummy method to be implemented in the derived classes public abstract void soun d();private String type; package testabstract;public class Cat exte nds Ani malpublic Cat(Stri ng aName)super(Cat); / Call the base con structor n ame = aName; / Supplied n ame breed = Unknown; / Default breed valuepublic Cat(Stri ng aName, String aBreed)super(Cat); / Call the base con structor n ame = aName;/ Supplied n amebreed = aBreed; / Supplied breed/ Return a String full of a cats detailspublic Stri ng toStri ng()return super.toString() + nits + name + the + breed;/ A miaowi ng methodpublic void soun d()System.out.pri ntln (Miiaooww);private String name; / Name of a catprivate String breed; / Cat breed package testabstract;public class Dog exte nds Ani malpublic Dog(Stri ng aName)super(Dog);con structorn ame = aName;/ Call thebase/ Supplied n amebreed = Unknown;/ Default breed valuepublic Dog(String aName, String aBreed)/ Call thebasesuper(Dog);con structorn ame = aName;breed = aBreed;/ Supplied n ame/ Supplied breed/ Prese nt a dogs details as a stri ngpublic Stri ng toStri ng()return super.toString() + nits + name + the + breed; / A bark ing method public void soun d()System.out.pri ntln (Woof Woof);/ Name of a Dog/ Dog breedprivate String name;private String breed; package testabstract;public class Duck exte nds Ani malpublic Duck(Stri ng aName)super(Duck); / Call the base con structor n ame = aName; / Supplied n ame breed = Unknown; / Default breed valuepublic Duck(Stri ng aName, String aBreed)super(Duck); / Call the base con structor n ame = aName;/ Supplied n amebreed = aBreed; / Supplied breed/ Retur n a String full of a ducks detailspublic Stri ng toStri ng()return super.toString() + nits + name + the + breed;/ A quack ing methodpublic void soun d()System.out.pri ntl n(Quack quackquack);private Stri ng n ame;/ Duck n ameprivate String breed; / Duck breed package testabstract;class Spaniel exte nds Dogpublic Spani el(Stri ng aName)super(aName, Spa ni el);package testabstract;import java.util.Ra ndom;public class TestAbstractpublic static void main(String args)/ Create an array of three differe nt ani mals An imal theA ni mals = new Dog(Rover, Poodle),new Cat(Max, Abyss in ia n),new Duck(Dafy,Aylesbury),new Spa ni el(Fido);Ani mal petChoice;/ Choice of pet/ Ran dom nu mberRandom select = new Random();gen erator/ Make five ran dom choices of petfor(i nt i = 0; i 5; i+) / Choose a ran dom ani mal as a petpetChoice = theA nimalsselect. nextl nt(theA nimals .len gth);System.out.pri ntln (nY our choice:n + petChoice);petChoice.so un d();/ Get the petsreacti on2、接口接口可以被用来实现类间多继承结构。接口内部只能定义public 的抽象方法和静态的、公有常量,因此所有的方法需要在子类中实现。接口定义方法:访问权限in terface接口名称 exte nds 父接口名 1,父接口名2,./接口体程序范例 testinterface:package test in terface;public class Cat impleme nts PetOutputpublic Cat(Stri ng aName)super(Cat);/ Call the base con structorn ame = aName; / Supplied n amebreed = Unknown; / Default breed valuepublic Cat(Stri ng aName, String aBreed)super(Cat);/ Call the base con structorn ame = aName;/ Supplied n amebreed = aBreed; / Supplied breed/ Return a String full of a cats details public Stri ng toStri ng()retur n n Its + n ame + the + breed;/ A miaowi ng methodpublic void soun d()System.out.pri ntln (Miiaooww);private String name; / Name of a catprivate String breed; / Cat breedpackage test in terface;public class Dog impleme nts PetOutputpublic Dog(Stri ng aName)n ame = aName;/ Supplied n amebreed = Unknown; / Default breed valuepublic Dog(String aName, String aBreed) n ame = aName; / Supplied n ame breed = aBreed; / Supplied breed/ Prese nt a dogs details as a stri ngpublic Stri ng toStri ng()return Its + n ame + the + breed;/ A bark ing methodpublic void soun d()System.out.pri ntln (Woof Woof);public String getName()return n ame;private Stri ng n ame; / Name of a dogprivate String breed; / Dog breed package test in terface;public class Duck impleme nts PetOutputpublic Duck(Stri ng aName)super(Duck); / Cal
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号