类与对象的创建
类与对象的关系
-
类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物。
- 动物,植物,手机,电脑…
- Person类,Pet类,Car类等,这些类都是用来描述/定义某一类具体的事物应该具备的特点和行为。
-
对象是抽象概念的具体实例
- 张三就是人的一个具体实例,张三家里的旺财就是狗的一个具体实例。
- 能够体现出特点,展现出功能的是具体的实例,而不是一个抽象的概念。
-
package com.oop.Demo02; //学生类 public class Student{ //属性:字段 String name;//null int age;//0 //方法 public void study(){ System.out.println(this.name+"在学习"); } } //学程序好?对世界进行更好地建模!
-
package com.oop.Demo02; //一个项目应该只存一个main方法 public class Aplication { public static void main(String[] args) { //类:抽象的,实例化 //类实例化后会返回一个自己的对象! //student对象就是一个Student类的具体实例! Student student = new Student(); Student student1 = new Student(); student.name = "徐航"; student.age =21; System.out.println(student.name); System.out.println(student.age); student1.name="梁欣悦"; student1.age=22; System.out.println(student1.name); System.out.println(student1.age); } }