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();
}
}