Java学习11
·
1. 标准实体类:Student(最核心格式)
java
运行
public class Student {
// 1. 成员变量(属性)
private int id;
private String name;
private int age;
private double score;
// 2. 无参构造方法
public Student() {
}
// 3. 全参构造方法
public Student(int id, String name, int age, double score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
// 4. getter & setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
// 5. 成员方法:打印信息
public void show() {
System.out.println("学号:" + id +
",姓名:" + name +
",年龄:" + age +
",成绩:" + score);
}
}
讲解
- private私有,只能在本类中访问,外部不能直接改,这就是封装。
- 构造方法
- 方法名和类名完全一样
- 没有返回值类型
- 作用:创建对象时给属性赋值
- this.xxx代表当前对象的成员变量,用来区分成员变量和局部参数重名。
- getter/setter外部间接访问、修改私有属性的唯一入口。
- show()普通成员方法,对象调用,用来打印自身信息。
2. 测试类:创建并使用 Student 对象
java
运行
public class TestStudent {
public static void main(String[] args) {
// 1. 用无参构造创建对象
Student stu1 = new Student();
stu1.setId(1);
stu1.setName("张三");
stu1.setAge(20);
stu1.setScore(95.5);
stu1.show();
// 2. 用全参构造创建对象(一步赋值)
Student stu2 = new Student(2, "李四", 21, 88.0);
stu2.show();
// 3. 单独获取某个属性
System.out.println("stu2 的姓名:" + stu2.getName());
}
}
讲解
- 创建对象
Student stu1 = new Student();左边是引用变量,指向堆里的真实对象。 - 无参 + set 赋值先空对象,再一个个 set 进去。
- 全参构造一步到位传完所有参数,更简洁。
- 多个对象互不干扰stu1 和 stu2 是两个独立对象,修改一个不影响另一个。
二、晚上练习:Phone 类(作业题完整代码)
Phone 类
java
运行
public class Phone {
private String brand;
private double price;
private String color;
public Phone() {
}
public Phone(String brand, double price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void call() {
System.out.println(brand + " 手机正在打电话");
}
public void sendMessage() {
System.out.println(brand + " 手机正在发短信");
}
}
讲解
- 属性:品牌、价格、颜色
- 无参、全参构造
- getter/setter
- 两个行为方法:打电话、发短信
测试:创建 3 个 Phone 对象
java
运行
public class TestPhone {
public static void main(String[] args) {
Phone p1 = new Phone("华为", 6999, "黑色");
Phone p2 = new Phone("小米", 4299, "白色");
Phone p3 = new Phone("苹果", 8999, "紫色");
p1.call();
p2.sendMessage();
System.out.println(p1.getBrand() + ":" + p1.getPrice() + " 元");
}
}
讲解
- 一次性创建 3 个不同对象
- 每个对象有自己独立的属性值
- 调用方法执行各自行为
三、晚上练习:Car 类(含启动、行驶方法)
java
运行
public class Car {
private String brand;
private double price;
public Car() {
}
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
// 行为1:启动
public void start() {
System.out.println(brand + " 汽车启动了");
}
// 行为2:行驶
public void run() {
System.out.println(brand + " 汽车正在行驶");
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
讲解
- 面向对象思想:对象 = 属性 + 行为
start()、run()是对象的功能方法- 外部通过
对象.start()让车做事
测试 Car
java
运行
public class TestCar {
public static void main(String[] args) {
Car c = new Car("奔驰", 300000);
c.start();
c.run();
}
}
四、封装总结(必须背会)
- 类:模板、设计图
- 对象:根据类创建的真实实例
- private:私有化,禁止外部直接访问
- getter/setter:提供安全的访问入口
- 构造方法:创建对象时初始化数据
- 成员方法:对象的行为功能
- 以前写
static:是面向过程,直接调用方法。 - 现在不写
static:是面向对象,必须先创建对象,再用对象调用方法。 - getXxx ():获取值(拿出来)
- setXxx ():设置值(存进去)
一句话核心
Student 类 = 模具 / 设计图TestStudent 类 = 生产车间 / 使用方
它们必须关联,不然你造不出对象!
🟦 Student(负责:定义属性 + 方法)
你在这里写:
- id
- name
- age
- score
- getter/setter
- show()
它只负责定义模板,不运行代码。
🟩 TestStudent(负责:创建对象 + 使用)
你在这里写:
- main 方法
- new Student()
- 调用 set、get、show
它只负责使用模板,不定义属性。
最后拓展一个知识点关于1.0E7实际上就是1.0*10的七次方
更多推荐


所有评论(0)