类与对象

【类与对象】

**1. 类与对象的概念:类是对所有相同属性和功能的个体的统称;对象是具体的个体,实物;

**2. 类与对象的关系:对象是类中的具体的个体,类是对象的统称;

**3. 类与对象的定义使用:

  类的定义:public class 类名{  }       

  类中属性的定义:public(访问权限)  属性名 属性名; 

  类中方法的定义:public 返回类型 方法名(参数属性  参数){   具体功能   }   

  创建对象:类名 对象名 = new 类名();    

  对象属性的引用:对象名.属性名;[ 方法中引用自己的属性直接写属性名 ]       // System.out.println(name+"在学习!");

  对象方法的引用:对象名.方法名(参数);

【实例】

【程序】

//定义类
public class student {
    // 定义类的属性
    public int garde;
    public int age = 10;
    public String name;

    // 定义类的方法
    public void setname(String n) {
        name = n;
    }

    public void study() {
        System.out.println(name + "在学习!");

    }

    public static void main(String[] args) {
        student stu = new student(); // 创建对象
        stu.setname("张三"); // 方法的引用
        stu.study();
    }
}

  //  其中student是一个类,所有的学生个体都属于student类,所有的学生个体都是student中的对象(张三)

【输出结果】

posted @ 2019-04-28 19:17  runzhi_z  阅读(113)  评论(0编辑  收藏  举报