赞
踩
类就是抽象,宏观上的,不占用内存
对象是类的具体实例,占用内存
把类比做吃饭,而对象就是吃的什么,米饭,苹果,这都是对象的具体实例
可以说类就是一件事的统称,而对象,是做这件事的具体动作。
- //创建一个学生类
- public class StudentKind1 {
- // 类里面有两个成员变量,
- // 一个是名字(name)和一个年龄,这时候还没有赋值
- String name;
- int age;
- }
//创建一个学生类
public class StudentKind1 {
// 类里面有两个成员变量,一个是名字(name)和一个年龄,这时候还没有赋值
String name;
int age;
// 建两个成员方法,一个输出名字,一个输出年龄
public void studentName(){
System.out.println("我的名字是:"+name);
}
public void studentAge(){
System.out.println("我今年"+age+"岁了");
}
}
public class StudentKind2 {
public static void main(String[] args) {
// 创建对象
StudentKind1 method = new StudentKind1();
// 通过对象.成员变量名,给成员变量name赋值
method.name = "张三";
// 给成员变量age赋值
method.age = 19;
// 通过对象.成员方法名,来调用成员方法
method.studentName();
method.studentAge();
}
}
- public class StudentKind2 {
- public static void main(String[] args) {
-
- StudentKind1 method = new StudentKind1();
- method.name = "张三";
- method.age = 19;
-
- // 多个对象指向
- StudentKind1 mothod2 = new StudentKind1();
- mothod2.name="李四";
- mothod2.age = 22;
- // 第一个对象,在调用
- method.studentName();
- method.studentAge();
- // 第一个个对象调用
- mothod2.studentName();
- mothod2.studentAge();
- }
-
- }

- public class StudentKind2 {
- public static void main(String[] args) {
-
-
- StudentKind1 method = new StudentKind1();
-
- method.name = "张三";
-
- method.age = 19;
-
- // 多个对象指向相同位置,这是把地址赋给了mothod2,
- // 所以说,mothod2改变,method也会改变
- StudentKind1 mothod2 = method;
- mothod2.name="李四";
- mothod2.age = 22;
- // 第一个对象,在调用
- method.studentName();
- method.studentAge();
- // 第一个个对象调用
- mothod2.studentName();
- mothod2.studentAge();
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。