12java基础继承

26.定义类Human,具有若干属性和功能;定义其子类Man、Woman;

在主类Test中分别创建子类、父类和上转型对象,并测试其特性。

 

package com.hry.test;

public class Human
{
    //成员属性
    private String name;
    private int age;
    private double height;
    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 double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    //成员方法
    public void speak()
    {
        System.out.println("你喜欢什么运动呢?");
    }
    public void yundong()
    {
        System.out.println("我喜欢打篮球");
    }
    
}
package com.hry.test;

public class Man extends Human
{
    //重写父类的方法
    public void speak()
    {
        System.out.println("我是男生,我叫"+this.getName()+"我的年龄是:"+this.getAge()+"我的身高为:"+this.getHeight());
    }
    public void yundong()
    {
        System.out.println("我最喜欢的运动是玩游戏,德玛西亚....");
    }
}
package com.,hry.test;

public class Woman extends Human
{
    //重写父类方法
    public void speak()
    {
        System.out.println("我是女生,我叫"+this.getName()+"我的年龄是:"+this.getAge()+"我的身高为:"+this.getHeight());
    }
    public void yundong()
    {
        System.out.println("我最喜欢的运动是羽毛球");
    }
}
package com.hry.test;

public class Text_human 
{

    public static void main(String[] args) 
    {
        //实例化man对象
        Man m = new Man();
        m.setName("张小明");
        m.setAge(18);
        m.setHeight(178);
        m.speak();
        m.yundong();
        
        //实例化woman对象
        Woman wm = new Woman();
        wm.setName("张小红");
        wm.setAge(16);
        wm.setHeight(160);
        wm.speak();
        wm.yundong(); 
        
        //实例化对象
        Human h=new Human();
        h.speak();
        h.yundong();
        
        //定义上转型对象
        Human hm=new Man();
        hm.setName("tom");
        hm.setAge(19);
        hm.setHeight(178);
        hm.speak();
        hm.yundong();
            
    }

}

 

27.编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish

和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。

 

package com.hry.test;

public class Animal {
    
    //属性
    
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    
    //功能
    public void eat()
    {
    }
    public void sleep()
    {
    }
    
    

}
package com.hry.test;

public class Fish extends Animal{
    
    public void eat()
    {
        System.out.println(getType()+"");
    }
    public void sleep()
    {
        System.out.println(getType()+"");
    }

}
package com.hry.test

public class Dog extends Animal{
    
    public void eat()
    {
        System.out.println(getType()+"");
    }
    public void sleep()
    {
        System.out.println(getType()+"");
    }

}
package com.hry.test

public class TestAnamal {
    
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
    
    Fish f=new Fish();
    f.setType("");
    f.eat();
    f.sleep();
    
    Dog d=new Dog();
    d.setType("");
    d.eat();
    d.sleep();
    
    

    }
}

 

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

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

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

和计算体积的方法。

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

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

 

class rectangle
{
private int x;
private int y;

rectangle(int x,int y)
{
this.x=x;
this.y=y;
}
public int acreage()
{
retrun x*y;
}
} 

class cuboid extends rectangle
{
private int z;
cuboid(int x,int y,int z)
{
super(x,y);
this.z=z;
}

public int cubage()
{
return x*y*z;
}
}

publicclass Test {
publicstaticvoid main(String[] args){

cuboid c =new cuboid(2,3,4);
int i = c.cubage();

System.out.println("体积:"+ i);
\ }


}

 

29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数

wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数

loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个

类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功

能。

 

class Vehicle {
 private int wheels;
 private float weight;
 protected Vehicle(int wheels, float weight){
  this.wheels = wheels;
  this.weight = weight;
 }
 public int getWheels() {
  return wheels;
 }
 public float getWeight() {
  return weight;
 }
 public void print(){
  System.out.println("汽车:");
  System.out.println("共有"+this.getWheels()+"个轮子");
  System.out.println("重量为"+this.getWeight()+"");
 }
}
class Car extends Vehicle{
 private int passenger_load;
 public Car(int wheels, float weight, int passenger_load) {
  super(wheels, weight);
  this.passenger_load = passenger_load;
 }
 public int getPassenger_load() {
  return passenger_load;
 }
 public void print(){
  System.out.println("小车:");
  System.out.println("共有"+this.getWheels()+"个轮子");
  System.out.println("重量为"+this.getWeight()+"");
  System.out.println("载人数为"+this.getPassenger_load()+"");
 }
}

class Truck extends Vehicle{
 private int passenger_load;
 private float payload;
 public Truck(int wheels, float weight, int passenger_load, float payload) {
  super(wheels, weight);
  this.passenger_load = passenger_load;
  this.payload = payload;
 }
 public int getPassenger_load() {
  return passenger_load;
 }
 public float getPayload() {
  return payload;
 }
 public void print(){
  System.out.println("卡车:");
  System.out.println("共有"+this.getWheels()+"个轮子");
  System.out.println("重量为"+this.getWeight()+"");
  System.out.println("载人数为"+this.getPassenger_load()+"");
  System.out.println("载重量为"+this.getPayload()+"");
 }
}

public class Test{
 public static void main(String args[]){
  Vehicle car = new Car(4, 3, 4);
  Vehicle truck = new Truck(6, 6, 2, 10);
  System.out.println("*****************************");
  car.print();
  System.out.println("*****************************");
  truck.print();
 }
 
} 

/*
 *output:
*****************************
小车:
共有4个轮子
重量为3.0吨
载人数为4人
*****************************
卡车:
共有6个轮子
重量为6.0吨
载人数为2人
载重量为10.0吨

 

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

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

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

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

1. interface Shape
{
    public abstract double area();
    public abstract double perimeter();
    public abstract double display();
}

2. class Circle implements Shape
{
        int x;//横坐标
        int y;//纵坐标
    double a;//半径
    public Circle (int x,int y,double a)//构造函数
    {
                this.x=x;
                this.y=y;
        this.a=a;
    }
    public double area()
    {
        return 3.14*a*a;
    }

        public double perimeter()
    {
        return 2*3.14*a;
    }

        public void display()
    {
        System.Out.println("此圆的圆心为a("+this.x+","+this.y+")");
            System.Out.println("此圆的半径为r="+this.a);

    }
}

3. 4. 就不写了,与圆类似,就多了个drow()函数,你可以去找找这个函数的实现

5. public static void main(String[]args)throws IOException 
{
      Circle C=new Circle(2,3,5);
      System.out.println("所求圆的面积S="+C.area());
      System.out.println("所求圆的周长L="+C.perimeter());
      C.display();
//其他的测试一样
}

 

posted @ 2016-09-22 16:30  hanruyue  阅读(429)  评论(0编辑  收藏  举报