java程序设计(要详细的编程)

发布网友 发布时间:2022-04-23 06:43

我来回答

2个回答

热心网友 时间:2022-06-16 20:30

public class Test {
public static void main(String[] args) {
Person person = new Student("Kevin", 25, "0001");
System.out.println(person);
}
}
class Person {
private String name;
private int age;

Person() {
}

Person(String name, int age) {
this.name = name;
this.age = age;
}

public String toString() {
return "姓名: " + this.name + " 年龄: " + this.age;
}
}
class Student extends Person {
private String stuNum;

public Student() {
}

public Student(String name, int age, String stuNum) {
super(name, age);// 调用父类的构造函数。
this.stuNum = stuNum;
}

public String toString() {
return super.toString() + " 学号: " + this.stuNum;
}
}

热心网友 时间:2022-06-16 20:31

class Person{
public String name;
public String age;
public Person(){
System.out.println("Person Initialization……");
}

public Person(String n , String a){
this.name = n;
this.age = a;
}

public void printInfo(){
System.out.println("the Name Is " + this.name + "; the age is" + this.age);
}
}

public class Student extends Person{
public long stuID;

public Student(){
System.out.println("Student Initialization……");
}

public Student(String n , String a,long id){
super(String n , String a);
this.stuID = id;
}

public void printInfo(){
System.out.println("the Name Is " + this.name + "; the age is" + this.age + "stuID" + this.stuID);
}

public static void main(String[] args){
Student s1 = new Student("stu1" , "20");
s1.printInfo();
s1.stuID = 20090609L;

Student s2 = new Student("stu2" , "20",20090610L);
s2.printInfo();
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com