java基础面向对象

package com.object.oriented;

/**
 *  电脑类 (内存、硬盘、显示器、CPU、显卡)
 * @author hp
 *
 */
public class Computer {
	String name;
	String memory;
	String harddisk;
	String display;
	String cpu;
	String videocard;
	
	
	public Computer(String name, String memory, String harddisk, String display, String cpu, String videocard) {
		this.name = name;
		this.memory = memory;
		this.harddisk = harddisk;
		this.display = display;
		this.cpu = cpu;
		this.videocard = videocard;
	}
	

	void run() {
		System.out.println("电脑是:"+name+"\t"+memory+"\t"+harddisk+"\t"+display+"\t"+cpu+"\t"+videocard);
	}
	
	public static void main(String[] args) {
		Computer hp= new Computer("惠普电脑","8G","1TB","夏普","i7","gf9800");
		hp.run();
		
		Computer sum= new Computer("三星电脑","16G","2TB","夏普","i6","gf8200");
		sum.run();
		
		Computer lenovo= new Computer("联想电脑","8G","2TB","联想屏幕","i7","gf9800");
		lenovo.run();
		
	}
	
}

第二种方法

package com.object.oriented;

/**
 *  电脑类 (内存、硬盘、显示器、CPU、显卡)
 * @author hp
 *
 */
final class ComputerConfig{
	static final Hp  Hp= new Hp(new ComputerConfig("惠普电脑","8G","1TB","夏普","i7","gf9800"));
	static final Sum  SUM=new Sum(new ComputerConfig("三星电脑","16G","2TB","夏普","i6","gf8200"));
	static final Lenovo  LENOVO= new Lenovo(new ComputerConfig("联想电脑","8G","2TB","联想屏幕","i7","gf9800"));	
	
	String name;
	String memory;
	String harddisk;
	String display;
	String cpu;
	String videocard;
	
	public ComputerConfig(String name, String memory, String harddisk, String display, String cpu, String videocard) {
		this.name = name;
		this.memory = memory;
		this.harddisk = harddisk;
		this.display = display;
		this.cpu = cpu;
		this.videocard = videocard;
	}
}

class Hp{
	ComputerConfig config;// config = new ComputerConfig("惠普电脑","8G","1TB","夏普","i7","gf9800")
	Hp(ComputerConfig config){ 
		this.config=config;
	}
	
	void run(){
			System.out.println("电脑是:"+config.name+"\t"+config.memory+"\t"+config.harddisk+"\t"+config.display+"\t"+config.cpu+"\t"+config.videocard);
	}
	
}
class Lenovo{
	
	ComputerConfig config;
	Lenovo(ComputerConfig config){
		this.config=config;
	}
	void run(){
		System.out.println("电脑是:"+config.name+"\t"+config.memory+"\t"+config.harddisk+"\t"+config.display+"\t"+config.cpu+"\t"+config.videocard);
	}
}
class Sum{
	ComputerConfig config;
	Sum(ComputerConfig config){
		this.config=config;
	}
	
	void run(){
		System.out.println("电脑是:"+config.name+"\t"+config.memory+"\t"+config.harddisk+"\t"+config.display+"\t"+config.cpu+"\t"+config.videocard);
	}
}



public class ComputerTest{
	
	
	public static void main(String[] args) {
		//TDD    测试驱动开发
		
		ComputerConfig.Hp.run();
		ComputerConfig.LENOVO.run();
		
	}
	

}

第三种方法

package com.object.oriented;

/**
 *  电脑类 (内存、硬盘、显示器、CPU、显卡)
 * @author hp
 *
 */
public class ComputerTwo {
	static final ComputerTwo HP= new ComputerTwo("惠普电脑","8G","1TB","夏普","i7","gf9800");
	static final ComputerTwo SUM= new ComputerTwo("三星电脑","16G","2TB","夏普","i6","gf8200");
	static final ComputerTwo LENOVO= new ComputerTwo("联想电脑","8G","2TB","联想屏幕","i7","gf9800");
	
	
	String name;
	String memory;
	String harddisk;
	String display;
	String cpu;
	String videocard;
	
	ComputerTwo c;//c= new ComputerTwo("三星电脑","16G","2TB","夏普","i6","gf8200");
	
	
	public ComputerTwo(String name, String memory, String harddisk, String display, String cpu, String videocard) {
		this.name = name;
		this.memory = memory;
		this.harddisk = harddisk;
		this.display = display;
		this.cpu = cpu;
		this.videocard = videocard;
	}
	
	public ComputerTwo(ComputerTwo c) {  //c= new ComputerTwo("三星电脑","16G","2TB","夏普","i6","gf8200");
		this.c=c;
	}
	

	void run() {
		System.out.println("电脑是:"+c.name+"\t"+c.memory+"\t"+c.harddisk+"\t"+c.display+"\t"+c.cpu+"\t"+c.videocard);
	}
	
	public static void main(String[] args) {
		//TDD    测试驱动开发
		ComputerTwo c = new ComputerTwo(HP); //Computer sum= new Computer(new Sum());
		c.run();

		
	}	

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值