很简单的java初级设计程序
发布网友
发布时间:2022-05-12 19:13
我来回答
共3个回答
热心网友
时间:2023-10-19 05:19
1.
Admin.java
public class Admin {
public static void main(String[] aa) {
Vehicle v1 = new Vehicle();
getResult(v1);
Vehicle v2 = new Vehicle(4, 1000);
getResult(v2);
}
private static void getResult(Vehicle v) {
System.out.println("载重量: " + v.getLoad());
System.out.println("速度: " + v.getSpeed());
System.out.println("自重: " + v.getWeight());
System.out.println("车轮个数:" + v.getWheels());
}
}
Vehicle.java
/**
* Transport的子类Vehicle
*
* @author xiaojianhx
* @data 2008-09-10
* @version 1.0
*/
public class Vehicle extends Transport {
/** 车轮个数 */
private int wheels;
/** 车重 */
private double weight;
/**
* 默认构造器
*/
public Vehicle() {
}
/**
* 构造方法
*
* @param wheels
* 车轮个数
* @param weight
* 车重
*/
public Vehicle(int wheels, double weight) {
this.wheels = wheels;
this.weight = weight;
}
/**
* 车重的取得
*
* @return double 车重
*/
public double getWeight() {
return weight;
}
/**
* 车重的设定
*
* @param weight
* 车重
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* 车轮个数的取得
*
* @return int 车轮个数
*/
public int getWheels() {
return wheels;
}
/**
* 车轮个数的设定
*
* @param wheels
* 车轮个数
*/
public void setWheels(int wheels) {
this.wheels = wheels;
}
}
Transport.java
/**
* 运输工具类
*
* @author xiaojianhx
* @data 2008-09-10
* @version 1.0
*/
public class Transport {
/** 速度 */
private double speed;
/** 载重量 */
private double load;
/**
* 默认构造器
*/
public Transport() {
}
/**
* 构造方法
*
* @param speed
* 速度
* @param load
* 载重量
*/
public Transport(double speed, double load) {
this.speed = speed;
this.load = load;
}
/**
* 载重量的取得
*
* @return double 载重量
*/
public double getLoad() {
return load;
}
/**
* 载重量的设定
*
* @param load
* 载重量
*/
public void setLoad(double load) {
this.load = load;
}
/**
* 速度的取得
*
* @return double 速度
*/
public double getSpeed() {
return speed;
}
/**
* 速度的设定
*
* @param speed
* 速度
*/
public void setSpeed(double speed) {
this.speed = speed;
}
}
2.
Teacher.java
public class Teacher {
private int no;
private String name;
private int age;
private String seminary;
public Teacher(int no, String name, int age, String seminary) {
this.no = no;
this.name = name;
this.age = age;
this.seminary = seminary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getSeminary() {
return seminary;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
}
Admin.java
public class Admin {
public static void main(String[] aa) {
int num = 0;
for (int i = 0; i < 5; i++) {
Teacher t = new Teacher(1 + i, "name" + i, 20 + i, "A" + i);
System.out.print(t.getNo() + " ");
System.out.print(t.getName() + " ");
System.out.print(t.getAge() + " ");
System.out.println(t.getSeminary() + " ");
num++;
}
System.out.println("共" + num + "位老师");
}
}
热心网友
时间:2023-10-19 05:19
--------------------------------------------
---1----------------------------------------
Transport.java
public class Transport {
public int speed;
public int load;
public int getLoad() {
return load;
}
public void setLoad(int load) {
this.load = load;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Transport(int speed, int load) {
this.speed = speed;
this.load = load;
}
public Transport() {
super();
}
}
-------------------------------------------
Vehicle.java
public class Vehicle extends Transport {
public int wheels;
public int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public Vehicle(int speed, int load) {
super(speed, load);
}
public Vehicle(int speed, int load, int wheels, int weight) {
super(speed, load);
this.wheels = wheels;
this.weight = weight;
}
}
-------------------------------------------
TestVehicle.java
public class TestVehicle {
public static void main(String[] args){
Vehicle vehicle = new Vehicle(180,50,4,100);
System.out.println(vehicle.getSpeed());
System.out.println(vehicle.getLoad());
System.out.println(vehicle.getWheels());
System.out.println(vehicle.getWeight());
vehicle.setSpeed(140);
vehicle.setLoad(60);
vehicle.setWheels(8);
vehicle.setWeight(110);
System.out.println(vehicle.getSpeed());
System.out.println(vehicle.getLoad());
System.out.println(vehicle.getWheels());
System.out.println(vehicle.getWeight());
}
}
-------------------------------------------
---2---------------------------------------
Teacher.java
public class Teacher {
public static int num;
public int no;
public String name;
public int age;
public String seminary;
public Teacher() {
super();
}
public Teacher(int no, String name, int age, String seminary) {
super();
this.no = no;
this.name = name;
this.age = age;
this.seminary = seminary;
}
public static int getNum() {
return num;
}
public static void setNum(int num) {
Teacher.num = num;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getSeminary() {
return seminary;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
}
---------------------------------------
TestTeacher.java
public class TestTeacher {
public static void main(String[] args){
Teacher t1 = new Teacher(1,"zhangsan",43,"计算机");
System.out.println(t1.getNo());
System.out.println(t1.getName());
System.out.println(t1.getAge());
System.out.println(t1.getSeminary());
t1.setName("wangwu");
t1.setAge(50);
t1.setSeminary("国际经贸");
System.out.println(t1.getNo());
System.out.println(t1.getName());
System.out.println(t1.getAge());
System.out.println(t1.getSeminary());
Teacher.setNum(Teacher.getNum() + 1);
System.out.println(Teacher.getNum());
Teacher t2 = new Teacher(2,"lisi",44,"经管");
Teacher.setNum(Teacher.getNum() + 1);
System.out.println(Teacher.getNum());
Teacher t3 = new Teacher(3,"zhouwu",45,"信息");
Teacher.setNum(Teacher.getNum() + 1);
System.out.println(Teacher.getNum());
}
}
热心网友
时间:2023-10-19 05:20
8313674---你个垃圾广告