定义类:

    1. package com;
    2. public class Computer {
    3. String name; //名称
    4. String type;//类型
    5. String cpu;
    6. String gpu;
    7. int ram;
    8. public Computer(String name,String type){//第二个构造器
    9. this.name=name;
    10. this.type=type;
    11. //如果成员变量和参数重名的时候,需要在成员变量前面使用this.
    12. };
    13. public void start(){
    14. System.out.println("名称为"+name+"类型为"+type+"cpu为"+cpu+"gpu为"+gpu+"内存为"+ram+"的电脑开机了");
    15. };
    16. public void shutDown(){
    17. System.out.println("名称为"+name+"类型为"+type+"cpu为"+cpu+"gpu为"+gpu+"内存为"+ram+"的电脑关机了");
    18. };
    19. }

    调用:

    1. package com;
    2. public class UuseComputer {
    3. public static void main(String[] args) {
    4. Computer computer=new Computer("联想","ssss");
    5. computer.start();
    6. computer.shutDown();
    7. }
    8. }

    //如果成员变量和参数重名的时候,需要在成员变量前面使用this.成员变量名