写代码:
public class Car { // 成员变量 String color; // 颜色 int speed; // 速度 int seat = 5; // 座位 // 成员方法 public void run(){ System.out.println("跑-------"); } public void fly(){ System.out.println("飞-------"); } public static void main(String[] args) { // int a = 10; // 写在方法里的变量,局部变量 /* 在面向对象的世界里,变量是没有市场的,这种变量被称为引用 java 分为两种数据类型: 1. 基本数据类型 2. 引用数据类型 String 和 我们创建的所有的类 */ // 创建对象,创建了一辆车,想用这辆车,需要用c来访问 Car c = new Car(); // 让车去跑,对象或者 引用.方法() c.run(); c.color = "绿色"; c.speed = 120; System.out.println(c.color); Car c2 = new Car(); c2.color = "红色"; c2.speed = 180; System.out.println(c.seat); System.out.println(c2.seat); System.out.println(c.color); System.out.println(c2.color); c.fly(); } }
原文:https://www.cnblogs.com/boyw/p/12932730.html