java写一个类

2025-06-27 12:52:52
推荐回答(1个)
回答1:

public class Test{
public static void main(String[] args) {
Student p1 = new Student("小明",22,178.6);
p1.study();
Worker w1 = new Worker("张三", 33, 82.2);
w1.working();
}
}
class Person{
protected String name;
protected int age ;
Person(){

}
Person(String name , int age){
this.name = name ;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void walk(){
System.out.println("...Person...walking...");
}
}
//
class Student extends Person{
private double height ;
Student(){
}
Student(String name,int age,double height){
super(name,age);
this.height = height;
}
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height ;
}
public void walk(){
System.out.println("...Student...walking...");
}
public void study(){
System.out.println(this.name+"正在学习,"+this.name+"已经有"+this.age+"岁了,"+this.name+"的身高是"+this.height+".");
}
}
//
//
class Worker extends Person{

private double weight;
Worker(){
}
Worker(String name,int age,double weight){
super(name,age);
this.weight= weight;
}
public void setWeight(double weight){
this.weight= weight;
}
public double getWeight(){
return weight;
}
public void walk(){
System.out.println("...Worker...walking...");
}
public void working(){
System.out.println(this.name+"正在工作,"+this.name+"已经有"+this.age+"岁了,"+this.name+"+人的体重是+"+this.weight+"公斤.");
}

}