类与对象的关系

类与对象的关系

类是一种抽象的数据类型,是对某一类事物整体描述,但是并能代表某一个具体的事物,

对象是抽象概念的具体实例

面向对象本质:以类的方式组织代码,以对象的组织封装数据。

student类

package oop.Test01;

/**
* @author 顾文杰
*/
public class Student {
   //属性:字段
   String name;
   int age;
   //方法
   public void study(){
       System.out.println(this.name+"在学习");
  }

  }
   //学程序的好处对世界进行更好的建模.
package oop.Test01;

public class Application {
   //一个项目应该只存在一个main方法
   public static void main(String[] args) {
       //类是抽象的,需要实例化
       Student xiaoming=new Student();
       Student xiaohong=new Student();
       xiaoming.name="小明";
       xiaoming.age=3;
       xiaohong.name="小红";
       xiaohong.age=12;
       xiaohong.study();
       System.out.println(xiaoming.name);
       System.out.println(xiaoming.age);
       System.out.println(xiaohong.name);
       System.out.println(xiaohong.age);

  }
}

 

posted @ 2021-07-15 00:09  一颗苹果!!  阅读(105)  评论(0)    收藏  举报