一、
1、
package com.wanxi;
public class WaterDispenser {
private String brand;
private String color;
private String capacity;
private String model;
public void show() {
Util.print("品牌: {1}, 颜色: {2}, 容量: {3}, 型号: {4}", brand, color, capacity, model);
}
public WaterDispenser() {
}
public WaterDispenser(String brand, String color, String capacity, String model) {
this.brand = brand;
this.color = color;
this.capacity = capacity;
this.model = model;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCapacity() {
return capacity;
}
public void setCapacity(String capacity) {
this.capacity = capacity;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
2、
package com.wanxi;
public class TestWaterDispenser {
public static void main(String[] args) {
WaterDispenser waterDispenser = new WaterDispenser("美的", "红色", "5L", "手动净水");
waterDispenser.show();
}
}
二、
1、
package com.wanxi;
public class Phone {
private String brand;
private String color;
private String price;
public Phone() {
}
public Phone(String brand, String color, String price) {
this.brand = brand;
this.color = color;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public void call() {
Util.print("正在使用价格为{1}{2}的{3}手机打电话", price, color, brand);
}
public void sendMessage() {
Util.print("正在使用价格为{1}{2}的{3}手机打电话", price, color, brand);
}
}
2、
package com.wanxi;
public class TestPhone {
public static void main(String[] args) {
Phone phone = new Phone();
phone.setPrice("3000元");
phone.setColor("黑色");
phone.setBrand("小米");
phone.call();
phone.sendMessage();
}
}
三、
1、
package com.wanxi;
public class Manager {
private String name;
private int id;
private int salary;
private int bonus;
public void work(){
Util.print("工号为{1}基本工资为{2}奖金为{3}的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....", id, salary, bonus);
}
public Manager() {
}
public Manager( int id, int salary, int bonus) {
this.id = id;
this.salary = salary;
this.bonus = bonus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
2、
package com.wanxi;
public class Coder {
private String name;
private int id;
private int salary;
public void work() {
Util.print("工号为{1}基本工资为{2}的程序员正在努力的写着代码......", id, salary);
}
public Coder() {
}
public Coder(int id, int salary) {
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
3、
package com.wanxi;
public class TestCoderManager {
public static void main(String[] args) {
Manager manager = new Manager(123, 15000, 6000);
manager.work();
Coder coder = new Coder(135, 10000);
coder.work();
}
}
01-面向对象介绍
面向对象重点学什么 ?
1. 学习自己如何设计对象
2. 学习已有的对象如何使用 --- API章节
02-类和对象
前提需知 : Java 中想要创建对象, 必须要先有类的存在
类的介绍 : 一组相关属性和行为的集合, 将其看做为是对象的设计图
类的组成 :
属性(名词描述) : 属性在代码中, 使用成员变量表示, 成员变量跟之前定义变量的格式一样, 只不过位置发生了改变, 放在了类中, 方法外.
行为(动词描述) : 行为在代码中, 使用成员方法表示, 成员方法跟之前定义方法的格式一样, 只不过去掉了static关键字
05-成员变量和局部变量的区别
1. 类中位置不同 :
成员变量: 方法外
局部变量: 方法中
2. 初始化值不同 :
成员变量: 有默认初始化值
局部变量: 没有默认初始化值, 使用之前必须完成赋值
3. 内存位置不同 :
成员变量: 堆内存
局部变量: 栈内存
4. 生命周期不同 :
成员变量: 随着对象的创建而存在, 随着对象的消失而消失
局部变量: 随着方法的调用而存在, 随着方法的运行结束而消失
5. 作用域 :
都在自己所属的大括号中有效
06-this关键字
情况 : 成员变量和局部变量重名了, 在使用的时候, 会根据就近原则, 优先使用局部变量
this 的作用 :
-
this可以使用点儿的形式调用本类的成员
-
this.本类成员变量
-
this.本类成员方法
this 的介绍 :
-
代表当前类对象的地址, 哪一个对象调用的方法, 方法中的this, 代表的就是哪一个对象
07-构造方法
-
介绍 :
-
构造方法 | 构造器
-
创建对象的时候, 要执行的方法
-
-
格式 :
-
方法名与类名相同, 大小写也必须一致
-
没有返回值类型, 连void都没有
-
没有具体的返回值 (方法中, 不能通过return语句带回结果)
-
08-封装
面向对象三大特征
-
封装
-
继承
-
多态
封装思想 :
* 隐藏实现细节, 仅对外暴露公共的访问方式
封装在代码中常见的体现 :
-
将属性抽取到类中, 是对零散的数据, 进行了封装
09-权限修饰符
-
private : 同一个类中
-
(default) : 同一个类中, 同一个包中
-
protected : 就业班学
-
public : 可以在任意位置访问
10-标准JavaBean类
-
成员变量全部 private
-
提供空参, 带参构造方法
-
针对于私有的成员变量, 编写对应的 setXxx 和 getXxx 方法