java繼承8個題

一、實現以下類之間的繼承關係,並編寫Music類來測試這些類。測試

public class Instrument {
    public void play(){
        System.out.println("彈奏樂器!");
    }
}
public class Wind extends Instrument{
    //複寫play方法
    public void play(){
        System.out.println("彈奏Wind!");
    }
    public void play2(){
        System.out.println("調用wind的play2");
    }
}
public class Brass extends Instrument {
    //複寫play方法
    public void play(){
        System.out.println("彈奏brass!");
    }
    public void play2(){
        System.out.println("調用brass的play2!");
    }
}
public class Music {
    public static void tune(Instrument i){
        i.play();
    }
    public static void main(String[] args){
        Wind w=new Wind();
        Brass b=new Brass();
        tune(w);
        tune(b);
    }
}

二、建立以下三個類:(People類中的三個方法分別輸出一些信息,ChinaPeopleAmericanPeople類重寫父類的三個方法)。this

public class People {
    protected double height;
    protected double weight;
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public void speakHello(){
        System.out.println("你好!!!");
    }
    public void averageHeight(){
        System.out.println("平均身高是:"+height);
    }
    public void averageWeight(){
        System.out.println("平均體重是:"+weight);
    }
}
public class ChinaPeople extends People {
    public void speakHello(){
        System.out.println("你好,我是中國人!");
    }
    public void averageHeight(){
        System.out.println("中國人的平均身高是:"+height);
    }
    public void averageWeight(){
        System.out.println("中國人的平均體重是:"+weight);
    }
    public void chinaGongfu(){
        System.out.println("我會中國功夫:坐如鐘,站如鬆,睡如弓!");
    }
}
public class AmericanPeople extends People {
    public void speakHello(){
        System.out.println("你好,我是美國人!");
    }
    public void averageHeight(){
        System.out.println("美國人的平均身高是:"+height);
    }
    public void averageWeight(){
        System.out.println("美國人的平均體重是:"+weight);
    }
    public void amercianBoxing(){
        System.out.println("我會美式拳擊:直拳,上勾拳!");
    }
}
public class TestPeople {
    public static void main(String[] args) {
        ChinaPeople chp=new ChinaPeople();
        chp.height=180;
        chp.weight=130;
        chp.speakHello();
        chp.averageHeight();
        chp.averageWeight();
        chp.chinaGongfu();
        AmericanPeople amp=new AmericanPeople();
        amp.setHeight(175);
        amp.setWeight(150);
        amp.speakHello();
        amp.averageHeight();
        amp.averageWeight();
        amp.amercianBoxing();
    }
}

三、編寫一個Java應用程序,該程序包括3個類:Monkey類、People類和主類E。要求:spa

(1) Monkey類中有個構造方法:Monkey (String s),而且有個public void speak()方法,在speak方法中輸出「咿咿呀呀......」的信息。設計

(2)People類是Monkey類的子類,在People類中重寫方法speak(),speak方法中輸出「小樣的,不錯嘛!會說話了!」的信息。3d

(3)People類中新增方法void think(),在think方法中輸出「別說話!認真思考!」的信息。code

(4)在主類Emain方法中建立MonkeyPeople類的對象類測試這2個類的功能。對象

public class Monkey {
    private String name;           //聲明屬性name
    //getter、setter方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    //構造方法    
    Monkey(String s){
        this.name=s;
    }
    //成員方法
    public void speak(){
        System.out.println("咿咿呀呀.........");
    }
}
public class People00 extends Monkey {
    People00(String s) {
        super(s);
    }
    //重寫speak()方法
    public void speak(){
        System.out.println("小樣的,不錯嘛!會說話了!");
    }
    void think(){
        System.out.println("別說話!認真思考!");
    }
}
public class E {
    public static void main(String[] args) {
        Monkey m=new Monkey("Toney");
        System.out.println("我是猴子"+m.getName());
        m.speak();
        People00 p=new People00("Tom");
        System.out.println("我是人類"+p.getName());
        p.speak();
        p.think();
    }
}

 四、定義類Human,具備若干屬性和功能;定義其子類ManWoman在主類Test中分別建立子類、父類和上轉型對象,並測試其特性。blog

public class Human {
    private String name;
    private int age;
    private String sex;
    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 String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public void speak(){
        System.out.println("I am a human.");
    }
    public void run(){
        System.out.println("I can run.");
    }
    public void getInfo(){
        System.out.println("個人名字是:"+getName()+" 個人年齡是:"+getAge()+" 個人性別是:"+getSex());
        Man m=new Man();
    }
}
public class Man extends Human{
    public void man(){
        if(getSex()=="男人"){
            System.out.println("I am a man.");
        }
        else{
            System.out.println("I am not a man.");
        }
    }
    public void run(){
        System.out.println("我是雄獅同樣強壯的男人.");
    }
}
public class Woman extends Human {
    public void woman(){
        if(getSex()=="女"){
            System.out.println("I am a woman.");
        }
        else{
            System.out.println("I am not a woman.");
        }
    }
}
public class TestHuman {
    public static void main(String[] agrs){
        Man m=new Man();
        m.setName("Dave");
        m.setAge(28);
        m.setSex("男");
        m.getInfo();
        m.speak();
        m.run();
        m.man();
        //隱式轉換
        //上轉型對象
        Human h=new Woman();
        h.setName("Jude");
        h.setAge(58);
        h.setSex("女");
        h.speak();
        h.run();
        h.getInfo();
        if(h instanceof Woman){
            Woman w=(Woman)h;
            w.woman();
        }
        else{
            System.out.println("類型不符!");
        }
    }
}

五、編寫一個Animal類,具備屬性:種類;具備功能:吃、睡。定義其子類FishDog,定義主類E,在其main方法中分別建立其對象並測試對象的特性。 繼承

public class Animal {
    private String zhonglei;
    private String name;
    public String getZhonglei() {
        return zhonglei;
    }
    public void setZhonglei(String zhonglei) {
        this.zhonglei = zhonglei;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void eat(){
        System.out.println("我須要覓食!");
    }
    public void sleep(){
        System.out.println("我須要睡覺!");
    }
    public void getInfo(){
        System.out.println("個人名字是:"+getName()+" 個人種類是:"+getZhonglei());
    }
}
public class Fish extends Animal {
    public void speak(){
        System.out.println("我是"+getZhonglei());
    }
    public void eat(){
        System.out.println("我吃魚食!");
    }
}
public class Dog extends Animal{
    public void speak(){
        System.out.println("我是"+getZhonglei());
    }
    public void eat(){
        System.out.println("我吃狗糧!");
    }
}
public class TestAnimal {
    public static void main(String[] args){
        Animal a=new Fish();
        a.setName("Tom");
        a.setZhonglei("動物");
        a.getInfo();
        a.eat();
        a.sleep();
        if(a instanceof Fish){
            Fish f=(Fish)a;
            f.setZhonglei("魚類");
            f.eat();
            f.getInfo();
        }
        else{
            System.out.println("類型不符!");
        }
        Dog d = new Dog();
        d.setName("哮天犬");
        d.setZhonglei("哺乳動物");
        d.getInfo();
        Animal aa=(Dog)d;
        aa.eat();
        aa.sleep();
    }
}

六、按要求編寫一個Java應用程序:ci

1)定義一個類,描述一個矩形,包含有長、寬兩種屬性,和計算面積方法。

2)編寫一個類,繼承自矩形類,同時該類描述長方體,具備長、寬、高屬性,和計算體積的方法。

3)編寫一個測試類,對以上兩個類進行測試,建立一個長方體,定義其長、寬、高,輸出其底面積和體積。

public class Juxing {
    private double chang;
    private double kuan;
    public double getChang() {
        return chang;
    }
    public void setChang(double chang) {
        this.chang = chang;
    }
    public double getKuan() {
        return kuan;
    }
    public void setKuan(double kuan) {
        this.kuan = kuan;
    }
    public double area(){
        return chang*kuan;
    }
}
public class ChangFangTi extends Juxing {
    private double gao;
    public double getGao() {
        return gao;
    }
    public void setGao(double gao) {
        this.gao = gao;
    }
    public double tiji(){
        return area()*gao;
    }
}
public class Test {
    public static void main(String[] args){
        ChangFangTi a=new ChangFangTi();
        a.setChang(12);
        a.setKuan(10);
        a.setGao(5);
        System.out.println("長方體的底面積是:"+a.area());
        System.out.println("長方體的體積是:"+a.tiji());
    }
}

七、編寫一個Java應用程序,設計一個汽車類Vehicle,包含的屬性有車輪個數wheels和車重weight。小車類CarVehicle的子類,其中包含的屬性有載人數loader。卡車類TruckCar類的子類,其中包含的屬性有載重量payload。每一個類都有構造方法和輸出相關數據的方法。最後,寫一個測試類來測試這些類的功能。

public class Vehicle {
    private int wheels;
    private double weight;
    public int getWheels() {
        return wheels;
    }
    public void setWheels(int wheels) {
        this.wheels = wheels;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    Vehicle(){
        
    }
    Vehicle(int wheels,double weight){
        this.wheels=wheels;
        this.weight=weight;
    }
    public void getInfo(){
        System.out.println("車輪的個數是:"+wheels+" 車重:"+weight);
    }
}
public class Car extends Vehicle {
    private int loader;
    public int getLoader() {
        return loader;
    }
    public void setLoader(int loader) {
        this.loader = loader;
    }
    Car(){
        
    }
    Car(int wheels,double weight,int loader){
        super(wheels,weight);
        this.loader=loader;
    }
    public void loader(){
        if(loader<=6){
            System.out.println("這輛車能載"+loader+"人");
        }
        else{
            System.out.println("超員了");
        }
    }
}
public class Truck extends Car {
    Truck(){
        super();
    }
    private double payload;
    public double getPayload() {
        return payload;
    }
    public void setPayload(double payload) {
        this.payload = payload;
    }
    public void payload(){
        if(payload<=1000){
            System.out.println("這輛車的載重是"+payload);
        }
        else{
            System.out.println("超重了");
        }
    }
}
public class Testcar {
    public static void main(String[] args){
        Car c=new Car(4,600,4);
        c.getInfo();
        c.loader();
        Truck t =new Truck();
        t.setLoader(3);
        t.setPayload(800);
        t.setWeight(1000);
        t.setWheels(6);
        t.getInfo();
        t.loader();
        t.payload();    
    }
}

八、編寫一個Shape類,具備屬性:周長和麪積;定義其子類三角形和矩形,分別具備求周長的方法。定義主類E,在其main方法中建立三角形和矩形類的對象,並賦給Shape類的對象ab,使用對象ab來測試其特性。

public class Shape {
    private int bian;
    private double zhouchang;
    private double mianji;
    public int getBian() {
        return bian;
    }
    public void setBian(int bian) {
        this.bian = bian;
    }
    public double getZhouchang() {
        return zhouchang;
    }
    public void setZhouchang(double zhouchang) {
        this.zhouchang = zhouchang;
    }
    public double getMianji() {
        return mianji;
    }
    public void setMianji(double mianji) {
        this.mianji = mianji;
    }
    public void bian(){
        System.out.println("您建立了一個"+bian+"邊形!");
    }
}
public class San extends Shape{
    double[] bianchang=new double[3];
    San(){
        
    }
    San(double a,double b, double c){
        bianchang[0]=a;
        bianchang[1]=b;
        bianchang[2]=c;
    }
    public void zhouchang(){
        if(getBian()==3&&bianchang[0]+bianchang[1]>bianchang[2]&&bianchang[0]-bianchang[1]<bianchang[2]){
            double sum=0;
            for(int i=0;i<bianchang.length;i++){
                sum+=bianchang[i];
                System.out.print(" 邊長"+(i+1)+"是"+ bianchang[i]);
            }
            System.out.println(" 三角形的周長是:"+sum);
        }
        else{
            System.out.println("這不是一個三角形");
        }
    }
}
public class Juxx extends Shape {
    double[] bianchang=new double[4];
    Juxx(){}
    Juxx(double a,double b,double c,double d){
        bianchang[0]=a;
        bianchang[1]=b;
        bianchang[2]=c;
        bianchang[3]=d;
    }
    public void zhouchang(){
        if(getBian()==4 && bianchang[0]==bianchang[2]&&bianchang[1]==bianchang[3]){
            double sum=0;
            for(int i=0;i<bianchang.length;i++){
                sum+=bianchang[i];
                System.out.print(" 邊長"+(i+1)+"是"+bianchang[i]);
            }
            System.out.println(" 矩形的周長是:"+sum);
        }
        else{
            System.out.println("這不是一個矩形");
        }
    }
}
public class TestE {
    public static void main(String[] args){
        Shape s=new Shape();
        s.setBian(5);
        s.bian();
        San sa=new San(3,4,5);
        sa.setBian(3);
        sa.zhouchang();
        Juxx ju=new Juxx(3,4,3,4);
        ju.setBian(4);
        ju.zhouchang();
    }
}

相關文章
相關標籤/搜索