Student s1 = new Student("201644001","张三","1997年02月03日");
时间: 2023-09-24 13:02:20 浏览: 38
这段代码是用 Java 语言创建了一个名为 s1 的对象,该对象的类型是 Student。并且该对象被初始化为一个学号为"201644001",姓名为"张三",出生日期为"1997年02月03日"的学生对象。可以通过以下代码来定义 Student 类:
```
public class Student {
private String stuNo;
private String name;
private String birthday;
public Student(String stuNo, String name, String birthday) {
this.stuNo = stuNo;
this.name = name;
this.birthday = birthday;
}
// getter and setter methods
}
```
在这个类中,我们定义了三个成员变量:stuNo、name、birthday,分别用于表示学号、姓名、出生日期。并且还定义了一个构造方法,用于初始化对象的属性值。同时,该类还提供了 getter 和 setter 方法,用于获取和设置对象的属性值。
相关问题
class Student{ String name; int age; int score=88; Student(String a1, int a2, int a3){ name=a1; age=a3; score=a2; } } 在使用该类时,已使用下面语句生成了该类的对象: Student s1,s2; s1=new student("张三",20,33); s2=new student(); 问题: 1)s1.name,s1.age,s1.score的值各是什么? 2)s2.name,s2.ag
1) 对象s1的属性值为:
- s1.name = "张三"
- s1.age = 33
- s1.score = 20
2) 对象s2的属性值为:
- s2.name = null (未赋值)
- s2.age = 0 (默认值)
- s2.score = 88 (默认值)
public class Student extends Person { Student() { System.out.println("I am a student."); } Student(String name) { super(name); System.out.println("I am a student."); } public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("张三"); } } class Person { private String name; Person() { System.out.println("I am a person."); } Person(String name) { System.out.println("I am a person, and my name is: " + name); } }
这段代码中定义了一个名为 Student 的类,它继承自另一个名为 Person 的类。Student 类有两个构造方法,一个无参构造方法和一个带有一个字符串参数的构造方法。在无参构造方法中,会先调用父类的无参构造方法,再输出 "I am a student.";在有参构造方法中,会先调用父类的带有一个字符串参数的构造方法,再输出 "I am a student."。在 main 方法中,分别创建了一个无参构造方法和一个带有一个字符串参数的构造方法的 Student 对象。运行这段代码将输出以下内容:
I am a person.
I am a student.
I am a person, and my name is: 张三
I am a student.