都是 Employee 类型的对象,并输出这5个对象的数据成员。要求:单位名称定义成类变量(也称静态变量,用static修饰);定义2个以上的构造函数,并用其完成属性值的初始化。 class Employee//定义Employee {
static String company=\成都信息工程学院\定义单位名称为静态变量 private String name, address, sex, postcode; Employee()//构造函数 {}
Employee(String name,String address,String sex,String postcode)//构造函数 { this.name = name;
this.address = address; this.sex = sex; this.postcode = postcode; }
public static void change_company(String c) //设置单位名称 { company = c; }
public void change_name(String name) //设置姓名 { this.name = name; }
public String getName() //获取姓名,由于name是私有成员,只能在本类访问,所以若要在其他类中获取姓名,必须依靠函数,其他私有成员同理 { return name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSex() { return sex; } public void setSex(String sex) {
值
}
this.sex = sex;
public String getpostcode() { return postcode; }
public void setpostcode(String postcode) { this.postcode = postcode; }
public void setEmp(String name,String address,String sex,String postcode)//为数据成员赋{ this.name = name; this.address = address; this.sex = sex; this.postcode = postcode; }
public void display() //输出职工各项数据 {
System.out.println(\单位:\ 姓名:\ 性别:\ 地址:\ 邮编:\ } }
class Test //定义测试类 { public static void main(String[] args){ Employee[] emp = new Employee[5];//定义数组,5个元素 for(int i=0;i<=4;i++) emp[i] = new Employee();//调用相关构造函数初始化对象 emp[0].setEmp(\李勇\女\引用数组元素,并调用相关函数为数据成员赋值 emp[1].setEmp(\张三\女\ emp[2].setEmp(\王芳\男\ emp[3].setEmp(\春春\男\ emp[4].setEmp(\吴璧\女\ for(int i = 0; i<4;i++)//以下输出各员工数据 {
}
emp[i].display();
System.out.println(\}
emp[0].change_name(\王五\改变姓名
Employee.change_company(\成都信息工程大学\emp[0].display();
System.out.println(\}
第四次上机
abstract class Vehicle{ private int wheels;//定义车轮个数 private double weight;//定义车重 public Vehicle (int wh, double we)//构造函数 { weight = we; wheels = wh; } public Vehicle ()//重载构造函数 { } }
public void show()//输出数据成员 { }
System.out.println(\车轮数为:\个\车重为:\吨\
class Car extends Vehicle//继承,定义Vehicle的子类Car { private int loader;//新增载客人数成员loader public Car(int wh, double we,int l)//子类构造函数 { super(wh,we);//调用父类构造函数 loader = l;//为新增成员赋值 }
public Car()//重载构造函数 { } public void show()//重写父类方法 { super.show();//调用父类的show()方法
System.out.println(\载客人数为:\个\ } }
class Truck extends Car{ //继承,定义Car的子类Truck private double payload;//新增子类成员载重量payload public Truck(int wh, double we,int l,double pl){//构造方法 super(wh, we, l);//调用父类构造方法 payload = pl; } public Truck(){//重载构造方法 }
public void show(){ //重写父类方法 super.show(); System.out.println(\载重量为:\吨\ } }
class Test //定义测试类 { public static void main(String[] args) { // Vehicle vv=new Car(3,2.2);//这句是错的,抽象类不能实例化,即不能new,可定义对象 Car cc=new Car(4,1.24,5);//定义Car对象 System.out.println(\的信息如下:\ cc.show(); System.out.println(\ Truck tt = new Truck(6, 2.1, 4, 10);//定义Truck对象 System.out.println(\的信息如下:\ tt.show(); } }
bstract class Shape//定义抽象类 { }
protected String name;//图形名称 public Shape(String name)//构造方法 { this.name = name; System.out.println(\名字:\}
abstract public double getArea(); abstract public double getLength();
class Circle extends Shape//定义子类圆Circle { private double radius;//新增子类数据成员 private double pi = 3.14;
public Circle(String shapename,double radius)//构造方法 { super(shapename);//调用父类构造方法 this.radius = radius; }
public void setRadius(double radius)//新增方法 {this.radius = radius;}
public double getArea(){//实现面积方法 return pi*radius*radius; }
public double getLength(){//实现周长方法 return 2*pi*radius; } }
class Rectangle extends Shape{//定义子类矩形Rectangle private double width; private double height;
public Rectangle(String shapename,double width, double height)//构造方法 { super(shapename); this.height = height; this.width = width; } public double getArea(){ return width*height; } public double getLength(){ return 2*(width + height); } }
class Test { public static void main(String[] args) { //Shape s1=new Shape(4,1.2);错误,抽象类不能实例化对象,但Shape cir = new Circle(\圆形\是对的,即对象的上转型对象 Circle cir = new Circle(\圆形\定义圆对象 System.out.println(\面积=\ System.out.println(\周长=\