/**11.构造方法的重载,定义一个名为Vehicle交通工具的基类,该类中应包含String类型的成员属性brand商标和color颜色还应包含成员方法run行驶在控制台显示“我已经开动了”和showlnfo显示信息:在控制台显示商标和颜色,并编写构造方法初始化其成员属性。编写Car小汽车类继承于Vehicle类增长int型成员属性seats座位,还应增长成员方法showCar在控制台显示小汽车的信息并编写构造方法。编写Truck卡车类继承于Vehicle类增长float型成员load载重,还应增长成员方法showTruck在控制台显示卡车的信息并编写构造方法。在main方法中测试以上各种。
*/
//Vehicle交通工具的基类
class Vehicle{
String brand;
String color;
public void run(){
System.out.println("我已经开动了");
}
public void showlnfo(){
System.out.println("商标:"+brand+"颜色:"+color);
}
public Vehicle(){
}
public Vehicle(String brand,String color){
this.brand=brand;
this.color=color;
}
}
//Car小汽车类继承于Vehicle类
class Car extends Vehicle{
int seats;
public Car(){
}
public Car(String brand,String color,int seats){
super(brand,color);
this.seats=seats;
}
public void showCar(){
showlnfo();
System.out.println("座位:"+seats);
}
}
//Truck卡车类继承于Vehicle类
class Truck extends Vehicle{
float load;
public Truck(){
}
public Truck(String brand,String color,float load){
super(brand,color);
this.load=load;
}
public void showTruck(){
showlnfo();
System.out.println("载重:"+load);
}
}
public class Test11{
public static void main(String[] args){
Vehicle v=new Vehicle("飞鸽","白色");
v.run();
v.showlnfo();
Car c=new Car("宝马","红色",6);
c.run();
c.showCar();
Truck t=new Truck("东方红","蓝色",7);
t.run();
t.showTruck();
}
}网络
/**12.构造方法与重载,定义一个网络用户类要处理的信息有用户ID、用户密码、email地址。在创建类的实例时,把以上三个信息都做为构造函数的参数输入,其中用户ID和用户密码是必须的,默认的email地址是用户ID加上字符串“@gameschool.com”
*/
class WebUser{
int id;
String password;
String email;
public WebUser(int id,String password){
this.id=id;
this.password=password;
//email默认值
email=id+"@gameschool.com";
}
public WebUser(int id,String password,String email){
this.id=id;
this.password=password;
this.email=email;
}
public void info(){
System.out.println("id:"+id+" 密码"+password+" email:"+email);
}
}
public class Test12{
public static void main(String[] args){
WebUser w=new WebUser(123,"monica888888");
w.info();
WebUser u=new WebUser(133,"monica888888","2833@qq.com");
u.info();
}
}函数