面向对象和定义类
面向对象
对象是实体,需要被创建,可以为我们做事情
类是规范,根据类的定义来创建对象
•对象
•表达东西或事件
•运行时响应消息
•类
•定义所有猫的属性
•就是java中的类型
•可以用来定义变量
对象=属性+服务
数据:属性或状态
操作:函数

把数据和对数据的操作放在一起-->封装
定义类
用自动贩卖机做实验
int price = 50;//价格 int balance;//余额 int total;//收进来的钱 void showprompl(){ System.out.println("Welcome"); } void isertMonry(int amount){ balance=balance+amount; } void showBalance(){ System.out.println(balance); } void getFood(){ if (balance>=price){ System.out.println("How are you"); balance = balance-price; total = total+price; } } public static void main(String[] args) { fanmai fanmai = new fanmai(); fanmai.showprompl(); System.out.println("你投了"+fanmai.price+"个币"); fanmai.isertMonry(100); fanmai.getFood(); fanmai.showBalance(); }
类是定义了这个类的所有对象长什么样,而对象是这个类的一个个的具体的实例

浙公网安备 33010602011771号