第一题
<span style="font-size:18px;">package 实验三第一题;
public class Animal
{
public String name;
public int legs;
void print()
{
System.out.println("这是一个:"+name+"\n"+"它有"+legs+"只腿");
System.out.println();
}
}
class Bird extends Animal
{
public String fly;
void Birdprint()
{
System.out.println("这是一个:"+name+"\n"+"它有"+legs+"只腿"+"\n"+"它"+fly+"飞");
System.out.println();
}
}
class Dog extends Animal
{
public String colour;
void Dogprint()
{
System.out.println("这是一个:"+name+"\n"+"它有"+legs+"只腿"+"\n"+"它的颜色是"+colour);
System.out.println();
}
}
class Person extends Animal
{
public String country;
public String ID;
public String sex;
void Personprint()
{
System.out.println("姓名:"+name+"\n"+"他/她有"+legs+"只腿"+"\n"+"他/她的国家是:"+country+"\n"+"他的性别是:"+sex);
System.out.println("身份证号是:"+ID);
System.out.println();
}
}
class student extends Person
{
public String stu_num;
public String Class;
void Personprint()
{
System.out.println("姓名:"+name);
System.out.println("它有"+legs+"只腿");
System.out.println("他/她的国家是:"+country);
System.out.println("他的性别是:"+sex);
System.out.println("他的学号是:"+stu_num);
System.out.println("他的身份证号是:"+ID);
System.out.println("他的班级:"+Class);
System.out.println();
}
}
</span>
测试代码
<span style="font-size:18px;">package 实验三第一题;
public class test {
public static void main(String[] args)
{
Animal a=new Animal();
a.name="鸟";
a.legs=2;
a.print();
Bird b=new Bird();
b.name="麻雀";
b.legs=2;
b.fly="会";
b.Birdprint();
Dog d=new Dog();
d.name="哈巴狗";
d.legs=4;
d.colour="黄色";
d.Dogprint();
Person p=new Person();
p.name="小明";
p.legs=2;
p.country="中国";
p.sex="男";
p.ID="37812687146927947091240";
p.Personprint();
student s=new student();
s.name="小红";
s.legs=2;
s.country="中国";
s.sex="女";
s.ID="192074988478326874687569";
s.stu_num="3212455535";
s.Class="网络工程141班";
s.Personprint();
}
}
</span>
建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求:汽车能够加速,减速,停车。再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖加速,减速的方法。
Auto类
package 实验3第二题;
public class Auto {
public int ty_num;
public String color;
public float self_weight;
public int speed;
public Auto()
{}
public Auto(int ty_num,String color,float self_weight,int speed)
{
this.ty_num=ty_num;
this.color=color;
this.self_weight=self_weight;
this.speed=speed;
}
public int getty_num()
{
return ty_num;
}
public String getcolor()
{
return color;
}
public float getself_weight()
{
return self_weight;
}
public float getspeed()
{
return speed;
}
public void in_speed()
{
if(speed<120)
{
System.out.println("可以加速!");
}
else
{
System.out.println("已达最大速度!");
}
}
public void de_speed()
{
if(speed>0)
{
System.out.println("可以减速");
}
else
{
System.out.println("已经停车");
}
}
public void stop()
{
speed=0;
System.out.println("速度已降为0!");
}
public void fun()
{
Auto one=new Auto();
Auto two=new Auto();
one.ty_num=6;
one.color="红色";
one.self_weight=2140;
one.speed=100;
two.ty_num=6;
two.color="黑色";
two.self_weight=1200;
two.speed=120;
one.in_speed();
one.de_speed();
System.out.println("轮胎数为:"+one.getty_num());
System.out.println("颜色为:"+one.getcolor());
System.out.println("自重为:"+one.getself_weight());
System.out.println("目前速度为:"+one.getspeed());
one.stop();
System.out.println();
two.in_speed();
two.de_speed();
System.out.println("轮胎数为:"+two.getty_num());
System.out.println("颜色为:"+two.getcolor());
System.out.println("自重为:"+two.getself_weight());
System.out.println("目前速度为:"+two.getspeed());
two.stop();
System.out.println();
}
}
Car类
package 实验3第二题;
class Car extends Auto {
private String have_air_condition;
private String have_CD;
public String have_air_condition()
{
return have_air_condition;
}
public String have_CD()
{
return have_CD;
}
public void carfun()
{
Car three=new Car();
three.ty_num=4;
three.color="红色";
three.self_weight=2000;
three.speed=120;
three.have_air_condition="有";
three.have_CD="有";
three.in_speed();
three.de_speed();
System.out.println("轮胎数为:"+three.getty_num());
System.out.println("颜色为:"+three.getcolor());
System.out.println("自重为:"+three.getself_weight());
System.out.println("目前速度为:"+three.getspeed());
System.out.println("是否有空调:"+three.have_air_condition());
System.out.println("是否有CD:"+three.have_CD());
three.stop();
System.out.println();
}
}
测试代码
package 实验3第二题;
public class AutoClass {
public static void main(String[] args)
{
Auto a =new Auto();
a.fun();
Car c=new Car();
c.carfun();
}
}