定义类:
package com;public class Computer {String name; //名称String type;//类型String cpu;String gpu;int ram;public Computer(String name,String type){//第二个构造器this.name=name;this.type=type;//如果成员变量和参数重名的时候,需要在成员变量前面使用this.};public void start(){System.out.println("名称为"+name+"类型为"+type+"cpu为"+cpu+"gpu为"+gpu+"内存为"+ram+"的电脑开机了");};public void shutDown(){System.out.println("名称为"+name+"类型为"+type+"cpu为"+cpu+"gpu为"+gpu+"内存为"+ram+"的电脑关机了");};}
调用:
package com;public class UuseComputer {public static void main(String[] args) {Computer computer=new Computer("联想","ssss");computer.start();computer.shutDown();}}
//如果成员变量和参数重名的时候,需要在成员变量前面使用this.成员变量名
