This commit is contained in:
seahi 2024-10-10 09:19:56 +08:00
parent a3e84dc516
commit c34ef6f2c2
3 changed files with 61 additions and 35 deletions

View File

@ -1,37 +1,37 @@
package chapter3; package chapter3;
class Student { //class Student {
String name; // String name;
int age; // int age;
String sex; // String sex;
//
void read() { // void read() {
System.out.println("大家好,我是" + name + ",我在看书!"); // System.out.println("大家好,我是" + name + ",我在看书!");
} // }
//
void introduce() { // void introduce() {
System.out.println("大家好,我叫" + name + ",我今年" + age + "岁了,我是" + sex + ""); // System.out.println("大家好,我叫" + name + ",我今年" + age + "岁了,我是" + sex + "");
} // }
} //}
//
public class Example01 { //public class Example01 {
public static void main(String[] args) { // public static void main(String[] args) {
Student s1 = new Student(); // Student s1 = new Student();
Student s2 = new Student(); // Student s2 = new Student();
//
s1.name = "小明"; // s1.name = "小明";
s1.sex = ""; // s1.sex = "";
s1.age = 20; // s1.age = 20;
//
//
s2.name = "小红"; // s2.name = "小红";
s2.sex = ""; // s2.sex = "";
s2.age = 19; // s2.age = 19;
//
s1.read(); // s1.read();
s2.read(); // s2.read();
//
s1.introduce(); // s1.introduce();
s2.introduce(); // s2.introduce();
} // }
} //}

View File

@ -8,6 +8,10 @@ class Book {
// this.id = 0; // this.id = 0;
// this.name = "未命名"; // this.name = "未命名";
// System.out.println("初始化:无参构造被调用!"); // System.out.println("初始化:无参构造被调用!");
// }
// public Book(int id, String name) {
// this.id = id;
// } // }
// 打印信息 // 打印信息

View File

@ -0,0 +1,22 @@
package chapter3;
class Student {
private String name;
private int age;
public Student(String name, int age) {
name = name;
age = age;
}
public void read() {
System.out.println("我是" + name + ",我今年" + age + "");
}
}
public class Example09 {
public static void main(String[] args) {
Student stu = new Student("小明", 15);
stu.read();
}
}