类,属性,方法,对象

 抽象:抽出像的部分,抽出共同的部分

通过抽象得到类,以类为模板产生对象

对象可以通过两种方式得到:实例化和声明

属性:类的特点(学生的id、姓名等)

光有属性没有方法叫做结构体

方法:类的功能(学生能学习,玩等)

package com.learn;

/**
 * @ author:森林之下
 * @ Date: Created in 16:16 2018/11/11
 * @ Description:方法、对象、类
 */
public class Student/*Student就是类*/ {

    /*属性*/
    int id = 0504;
    String name = "成龙";
    int age = 10;

    /*对Computer类进行声明得到comp对象*/
    Computer comp;

    /*定义一个study方法*/
    public void study(){
        /*通过comp对象调用Computer类的brand属性*/
        System.out.println("i love study 我使用的电脑品牌是:"+comp.brand);
    }

    /*定义一个play方法*/
    public void play(){
        System.out.println("i love play games");
    }

    /*创建main方法,main方法是程序执行的入口,必须要有*/
    public static void main(String[] args) {
        /*以Student类为模板实例化一个stu对象*/
        Student stu = new Student();
        /*以Computer类为模板实例化一个c1对象*/
        Computer c1 = new Computer();
        /*对brand属性进行赋值*/
        c1.brand = "ThinkPad";
        stu.comp = c1;
        /*调用Student类的study方法*/
        stu.study();

        /*调用Student类的age属性*/
        System.out.println("更改前的age:"+stu.age);

        /*通过stu对象调用Student类的age属性,并对age进行更改(赋值)*/
        stu.age = 20;
        System.out.println("更改后的age:"+stu.age);
    }
}
/*定义一个Computer类*/
class Computer{
    /*Computer类的brand属性*/
    String brand;
}

 

猜你喜欢

转载自my.oschina.net/u/3841003/blog/2874679