继承上机

package bbb;

    class Instrument{
         public void play(){
         System.out.println("输出弹奏乐器!");
         }
         }

        class Wind extends Instrument{
         public void play(){
         System.out.println("弹奏wind!");
         }
         public void play2(){
         System.out.println("输出wind的play2()");
         }
         }

        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){
         music i=new music();
         Wind s=new Wind();
         Brass c=new Brass();
         Instrument e=new Instrument();
         i.tune(e);
         i.tune(s);
         i.tune(c);

         }}

2、编写一个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 bbb;

class Monkey{
Monkey (String s){
}
public void speak(){
System.out.println("咿咿呀呀......");
}}
class People extends Monkey{
People(String s) {
super(s);
// TODO 自动生成的构造函数存根
}
public void speak(){
   System.out.println("小样的,不错嘛!会说话了");
}   
public void think(){
   System.out.println("别说话!认真思考!");
}

}

public class baby {
public static void main(String[]args){
Monkey s1=new Monkey(" ");
s1.speak();
People s2=new People(" ");
s2.speak();
s2.think();
}
}

4、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。

 代码:

//设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。
//小车类Car是Vehicle的子类,其中包含的属性有载人数loader。
//卡车类Truck是Car类的子类,其中包含的属性有载重量payload。
//每个类都有构造方法和输出相关数据的方法。
//最后,写一个测试类来测试这些类的功能。
package Person;
import java.util.*;

class Vehicle{
    int wheels;
    double weight;
    
    public void print(){
        System.out.println("请依次输入车轮的个数、车重为:");
        input();
        System.out.println("车轮的个数为:"+wheels+"   车重为:"+weight);
    }
    public void input(){
         Scanner sc = new Scanner(System.in);
         wheels=sc.nextInt();
         weight=sc.nextDouble();
         }
}

class Car extends Vehicle{
    int loader;
    
    public void print(){
        System.out.println("请输入载人数为:");
        input();
        System.out.println("载人数为:"+loader);
    }
    public void input(){
         Scanner sc = new Scanner(System.in);
         loader=sc.nextInt();
         }
}

class Truck extends Car{
    double payload;
    
    public void print(){
        System.out.println("请输入有载重量为:");
        input();
        System.out.println("有载重量为:"+payload);
}
    public void input(){
        Scanner sc = new Scanner(System.in);
        payload=sc.nextInt();
        }
}

public class TestCar {
    public static void main(String[] args) {
        Vehicle v = new Vehicle();
        Car c = new Car();
        Truck t = new Truck();
        
        v.print();
        c.print();
        t.print();
    }
}

 

posted @ 2019-05-11 23:08  姚中意  阅读(120)  评论(0编辑  收藏  举报