类与对象的关系
- 类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物。
- 动物、植物、手机、电脑…
- Person类、Pet类、Car类等,这些类都是用来描述/定义某一类具体的事物应该具备的特点和行为
- 对象是抽象概念的具体实例
- 张三就是人的一个具体实例,张三家里的旺财就是狗的一个具体实例
- 能够体现出特点,展现出功能的是具体的实例,而不是一个抽象的概念
- 使用new关键字创建对象
package com.oop.demo02;
public class Application {
public static void main(String[] args) {
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name="小明";
xiaoming.age=18;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
xiaohong.name="小红";
xiaohong.age=18;
System.out.println(xiaohong.name);
System.out.println(xiaohong.age);
}
}
package com.oop.demo02;
public class Student {
String name;
int age;
public void study() {
System.out.println(this.name+"在学习");
}
}
