Java-继承,多态练习

1,实现如下类之间的继承关系,并编写Music类来测试这些类。

复制代码
package com.chuoji.text01;

public class Instrument {
    
    public void play()
    {
        System.out.println("弹奏音乐");
    }
}
复制代码
public class Brass extends Instrument {

    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);
     }
    
}
复制代码
复制代码
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);
     }
    
}
复制代码
 
 
 
 
2。

创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法)。

 

复制代码
package zhongqiuzuoye;

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()
    {
    }
    public void averageHeight()
    {
    }
    public void averageWeight()
    {        
    }

}
复制代码
复制代码
package zhongqiuzuoye;

public class ChinaPeople extends People{
    public void chinaGoufu()
    {
        System.out.println("坐如钟,站如松");
    }
    public void speakHello()
    {
        System.out.println("你好");
    }
    public void averageHeight()
    {
        System.out.println("中国人平均身高为170cm");
    }
    public void averageWeight()
    {
        System.out.println("中国人平均体重是70kg");
    }

    

}
复制代码
复制代码
package zhongqiuzuoye;

public class AmericnPeople extends People{
    public void chinaGoufu()
    {
        System.out.println("直拳");
    }    
    public void speakHello()
    {
        System.out.println("hello");
    }
    public void averageHeight()
    {
        System.out.println("美国人平均身高为180cm");
    }
    public void averageWeight()
    {
        System.out.println("美国人平均体重是80kg");
    }

}
复制代码
复制代码
package zhongqiuzuoye;

public class TestPeople {
    
    public static void main(String[] args) {
        
        ChinaPeople c =new ChinaPeople();
        c.speakHello();
        c.averageHeight();
        c.averageWeight();
        
        AmericnPeople a=new AmericnPeople();
        a.speakHello();
        a.averageHeight();
        a.averageWeight();


}
}
复制代码

 

 

3.

编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类

E。要求:

(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()

方法,在speak方法中输出“咿咿呀呀......”的信息。

(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法

中输出“小样的,不错嘛!会说话了!”的信息。

(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”

的信息。

(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功

能。

父类:

复制代码
package com.lianxi2;

public class Monkey {
    
    private String s;
    
    public String getS() {
        return s;
    }


    public void setS(String s) {
        this.s = s;
    }


    public Monkey(String s)
    {
        this.s=s;
    }
    

    public Monkey() {
        super();
    }


    public void speak()
    {
        System.out.println("咿咿呀呀......");
    }
}
复制代码

子类:

复制代码
package com.lianxi2;

public class People extends Monkey {
    private String st;
    
    
    public People(String s, String st) {
        super(s);
        this.st = st;
    }

    public void speak()
    {
        System.out.println("小样的,不错嘛!会说话了!");
        
    }
    
    public void think()
    {
        System.out.println("别说话!认真思考!");
    }
}
复制代码

测试:

复制代码
package com.lianxi2;

public class M {

    public static void main(String[] args) {
        People pe=new People("你好","傻子");
        pe.speak();
        pe.think();
        Monkey m=new Monkey();
        m.speak();
    }

}
复制代码

结果:

 

 

 

4

定义类Human,具有若干属性和功能;定义其子类Man、Woman; 在主类Test中分别创建子类、父类和上转型对象,并测试其特性。

父类:

复制代码
package com.lianxi3;

public class Human {
    private String speak;
    private String jump;
    public String getSpeak() {
        return speak;
    }
    public void setSpeak(String speak) {
        this.speak = speak;
    }
    public String getJump() {
        return jump;
    }
    public void setJump(String jump) {
        this.jump = jump;
    }
    public Human(String speak, String jump) {
        super();
        this.speak = speak;
        this.jump = jump;
    }
    public Human() {
        super();
    }
    
    public void Jump()
    {
        System.out.println("跳:"+jump);
    }
    public void Speak()
    {
        System.out.println("说:"+speak);
    }
    public void as()
    {
        System.out.println("我能工作");
    }

}
复制代码

子类:

复制代码
package com.lianxi3;

public class Man extends Human {
    private String shui;
    

    public String getShui() {
        return shui;
    }


    public void setShui(String shui) {
        this.shui = shui;
    }


    
    
    public void Shui()
    {
        System.out.println("睡"+shui);
    }
    

}
复制代码

测试:

复制代码
package com.lianxi3;

public class Test {

    public static void main(String[] args) {
        Man m=new Man();
        m.setJump("低");
        m.setShui("长");
        m.setSpeak("粗");
        System.out.println("声音:"+m.getSpeak()+" 跳:"+m.getJump()+" 睡:"+m.getShui());
        
        //向上转型
        Human h=new Man();
        h.as();  //如果方法被重写,则调用子类的函数
        
        
        //向下转型
        Object ob=new Man();//先转形成等级最高的object
        
        Man ma=(Man)ob;  //在转型所需要的类型
        
        
    }

}
复制代码

 

 

5

28.按要求编写一个Java应用程序:

(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。

(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,

和计算体积的方法。

(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、

宽、高,输出其底面积和体积。

父类:

复制代码
package com.lianxi4;

public class Juxing {

    private double     chang;
    private double kuan;
    
    
    
    public double getChang() {
        return chang;
    }


    public void setChang(double chang) {
        this.chang = chang;
    }


    public Juxing(double chang, double kuan) {
        super();
        this.chang = chang;
        this.kuan = kuan;
    }
    

    public Juxing() {
        super();
    }


    public double mianJi()
    {
        return chang*kuan;
        
    }


}
复制代码

子类:

复制代码
package com.lianxi4;

public class Tiji extends Juxing {
     private double gao;

    public Tiji(double chang, double kuan, double gao) {
        super(chang, kuan);
        this.gao = gao;
    }
     
    public double tiJi()
    {
         return super.mianJi()*gao;
        
    }

}
复制代码

测试:

复制代码
package com.lianxi4;

public class Test {

    public static void main(String[] args) {
        Tiji ti=new Tiji(5,6,7);
        System.out.println("面积:"+ti.mianJi());
        System.out.println("体积:"+ti.tiJi());

    }

}
复制代码

结果:

 

 

 

6

编写一个Shape类,具有属性:周长和面积;

定义其子类三角形和矩形,分别具有求周长的方法。

定义主类E,在其main方法中创建三角形和矩形类的对象,

并赋给Shape类的对象a、b,使用对象a、b来测试其特性。

父类:

复制代码
package com.lianxi5;

//抽象类
public abstract class Shape {
    private double mianJi;
    private double zhouchang;
    public double getMianJi() {
        return mianJi;
    }
    public void setMianJi(double mianJi) {
        this.mianJi = mianJi;
    }
    public double getZhouchang() {
        return zhouchang;
    }
    public void setZhouchang(double zhouchang) {
        this.zhouchang = zhouchang;
    }
    
    public abstract double zhouchang();
    
    
    

}
复制代码

子类;

复制代码
package com.lianxi5;

public class Zc extends Shape {
    private double x;
    private double y;
    private double z;
    

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getZ() {
        return z;
    }

    public void setZ(double z) {
        this.z = z;
    }

    @Override
    public double zhouchang() {
        if((x+y)>z&&(x+z)>y&&(y+z)>x)
        {
            return x+y+z;
        }
        else
        {
        return 0;
        }
        
    
    }


}
复制代码
复制代码
package com.lianxi5;

public class Jxzc extends Shape {
    private double x;
    private double y;
    

    public double getX() {
        return x;
    }


    public void setX(double x) {
        this.x = x;
    }


    public double getY() {
        return y;
    }


    public void setY(double y) {
        this.y = y;
    }


    @Override
    public double zhouchang() {
        
        return (x+y)*2;
    }

}
复制代码

测试;

复制代码
package com.lianxi5;

public class Test {

    public static void main(String[] args) {
        Zc z=new Zc();
        Jxzc j=new Jxzc();
        z.setX(7);
        z.setY(8);
        z.setZ(9);
        System.out.println(z.zhouchang());
        
        

    }

}
复制代码

结果:

 

 
posted @ 2018-10-08 06:08  代码缔造的帝国  阅读(1299)  评论(0编辑  收藏  举报