feat(class15): 类,无参构造方法,有参构造方法,创建对象
- 在 constructor包中添加 Student 类,包含多个构造方法和属性 - 在 object 包中添加 Student 类,包含成绩相关方法 - 添加 Test 类测试 constructor 包中的 Student 类- 添加 Test2 类测试 object包中的 Student 类
This commit is contained in:
21
src/class15/constructor/Student.java
Normal file
21
src/class15/constructor/Student.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package class15.constructor;
|
||||
|
||||
public class Student {
|
||||
String name;
|
||||
int age;
|
||||
char sex;
|
||||
public Student(){
|
||||
System.out.println("无参构造方法");
|
||||
}
|
||||
public Student(String n){
|
||||
System.out.println("有参构造方法");
|
||||
name = n;
|
||||
}
|
||||
public Student(String n ,int a,char s){
|
||||
System.out.println("有参构造方法");
|
||||
name = n;
|
||||
age = a;
|
||||
sex = s;
|
||||
}
|
||||
|
||||
}
|
||||
26
src/class15/constructor/Test.java
Normal file
26
src/class15/constructor/Test.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package class15.constructor;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
Student s1 = new Student();
|
||||
Student s2 = new Student("小王");
|
||||
System.out.println(s1.name);
|
||||
System.out.println(s2.name);
|
||||
System.out.println("==========");
|
||||
|
||||
|
||||
Student t1 = new Student();
|
||||
t1.name = "石轩";
|
||||
t1.age = 18;
|
||||
t1.sex = '男';
|
||||
System.out.println(t1.name);
|
||||
System.out.println(t1.age);
|
||||
System.out.println(t1.sex);
|
||||
|
||||
System.out.println("==========");
|
||||
Student t2 = new Student("d老师", 18,'男');
|
||||
System.out.println(t2.name);
|
||||
System.out.println(t2.age);
|
||||
System.out.println(t2.sex);
|
||||
}
|
||||
}
|
||||
13
src/class15/object/Student.java
Normal file
13
src/class15/object/Student.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package class15.object;
|
||||
|
||||
public class Student {
|
||||
String name;
|
||||
double chinese;
|
||||
double math;
|
||||
public void printAllScore(){
|
||||
System.out.println(name+"的总成绩是:"+(chinese+math));
|
||||
}
|
||||
public void printAverageScore(){
|
||||
System.out.println(name+"的平均成绩是:"+(chinese+math)/2);
|
||||
}
|
||||
}
|
||||
20
src/class15/object/Test2.java
Normal file
20
src/class15/object/Test2.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package class15.object;
|
||||
|
||||
public class Test2 {
|
||||
public static void main(String[] args) {
|
||||
Student s1 = new Student();
|
||||
s1.name = "波妞";
|
||||
s1.chinese = 100;
|
||||
s1.math = 100;
|
||||
s1.printAllScore();
|
||||
s1.printAverageScore();
|
||||
|
||||
Student s2 = new Student();
|
||||
s2.name = "小王";
|
||||
s2.chinese = 100;
|
||||
s2.math = 100;
|
||||
s2.printAllScore();
|
||||
s2.printAverageScore();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user