类和对象的创建
类和对象的创建
类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是不能代表某一个具体的事物。
- 动物,植物,手机,电脑…
- Person类,Per类,Car类等,这些类都是用来描述/定义某一类具体事物应该具备的特点和行为
对象就是抽象概念的具体实例
- 例如张三就是人的一个具体实例,张三家里的旺财就是狗的一个具体实例
- 能够体现出特点,展现出功能的是具体的实例,而不是一个抽象的概念。
package com.gan.base.Demo02;
//学生类
public class Student {//属性,字段String name;//默认值为nullint age;//默认值为0//方法public void study(){System.out.println(this.name+"正在学习");}}
package com.gan.base.Demo02;
//一个项目应该只要一个main方法
public class Application {public static void main(String[] args) {//类:抽象的,实例化//类实例化后返回一个自己的对象Student student = new Student();//student对象就是一个Student类的具体实例!System.out.println(student.name);System.out.println(student.age);student.name="小明";student.age=18;System.out.println(student.name);System.out.println(student.age);}
}
这边在同一个包下的实现对其他类的调用,使用new关键字创建对象,对创建好的对象有进行默认的初始化,上面就是在对创建好的对象进行赋值前进后分别输出展示不同的结果。
输出结果为:
null
0
小明
18
null 和0 就是原来对象的默认值