学习打卡第五天
Posted sucker
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习打卡第五天相关的知识,希望对你有一定的参考价值。
1 package com.coin.concept; 2 3 public class Component { 4 private String name; 5 private int price; 6 public Component(String name , int price ) 7 { 8 this.name = name ; 9 this.price = price ; 10 } 11 public String description() 12 { 13 StringBuilder s = new StringBuilder(); 14 s.append("Name {") 15 .append(name) 16 .append("} Price{") 17 .append(price) 18 .append("} "); 19 return s.toString(); 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 public int getPrice() { 28 return price; 29 } 30 public void setPrice(int price) { 31 this.price = price; 32 } 33 public static void main(String[] args) { 34 // TODO Auto-generated method stub 35 36 } 37 38 }
1 package com.coin.concept; 2 3 public interface Workable { 4 void work(); 5 6 }
1 package com.coin.imp; 2 import com.coin.type.*; 3 public class AMDCPU extends CPU { 4 public AMDCPU (String name , int price , int codeNumber ) 5 { 6 super(name , price , codeNumber); 7 } 8 public void work() 9 { 10 System.out.println("This is CMD CPU working"); 11 } 12 }
1 package com.coin.imp; 2 import com.coin.type.*; 3 public class IntelCPU extends CPU { 4 public IntelCPU(String name , int price , int codeNumber) 5 { 6 super(name , price , codeNumber ); 7 } 8 public void work() 9 { 10 System.out.println("This is Inter CPU working"); 11 } 12 13 }
1 package com.coin.imp; 2 import com.coin.type.*; 3 public class SamsungDisk extends Disk { 4 public SamsungDisk(String name , int price , int capacity) 5 { 6 super(name , price ,capacity); 7 } 8 public void work() 9 { 10 System.out.println("This is Samsung Disk working"); 11 } 12 }
1 package com.coin.imp; 2 import com.coin.type.*; 3 public class SeagateDisk extends Disk { 4 public SeagateDisk(String name , int price , int capacity) 5 { 6 super(name , price , capacity); 7 } 8 public void work() 9 { 10 System.out.println("This is Seagate Disk working"); 11 } 12 }
1 package com.coin.type; 2 import com.coin.concept.*; 3 public abstract class CPU extends Component implements Workable { 4 private int codeNumber; 5 public CPU(String name, int price) 6 { 7 super(name, price); 8 } 9 public CPU(String name , int price , int codeNumber) 10 { 11 super(name , price); 12 this.codeNumber = codeNumber; 13 } 14 public abstract void work(); 15 @Override 16 public String description() 17 { 18 StringBuilder s = new StringBuilder(); 19 s.append(super.description()) 20 .append("CodeNumber:") 21 .append(codeNumber) 22 .append(" "); 23 return s.toString(); 24 } 25 }
1 package com.coin.type; 2 import com.coin.concept.*; 3 public abstract class Disk extends Component implements Workable { 4 private int capacity; 5 public Disk(String name, int price) 6 { 7 super(name, price); 8 } 9 public Disk(String name , int price , int capacity) 10 { 11 super(name , price ); 12 this.capacity = capacity ; 13 } 14 @Override 15 public String description() 16 { 17 StringBuilder s = new StringBuilder(); 18 s.append(super.description()) 19 .append("capacity:") 20 .append(capacity) 21 .append("MB") 22 .append(" "); 23 return s.toString(); 24 } 25 public abstract void work(); 26 27 }
1 package com.coin.product; 2 import com.coin.concept.*; 3 import com.coin.type.*; 4 public class Computer extends Component implements Workable { 5 private CPU cpu; 6 private Disk disk; 7 public Computer(String name , CPU cpu , Disk disk ) 8 { 9 super(name , -1 ); 10 this.cpu = cpu; 11 this.disk = disk; 12 super.setPrice(cpu.getPrice()+disk.getPrice()); 13 } 14 @Override 15 public String description() 16 { 17 StringBuilder s = new StringBuilder(); 18 s.append(super.description()) 19 .append(" ") 20 .append(cpu.description()) 21 .append(disk.description()); 22 23 return s.toString(); 24 } 25 public void work() 26 { 27 cpu.work(); 28 disk.work(); 29 } 30 }
1 package com.coin.product; 2 import com.coin.type.*; 3 import com.coin.imp.*; 4 public class ComputerStore { 5 6 public static void main(String[] args) { 7 //定义第一台电脑的配件 8 CPU intelCpu = new IntelCPU("Intel",1800,6); 9 Disk seagate = new SeagateDisk("Seagate",100,8192); 10 11 //组装第一台电脑 12 Computer computer1 = new Computer("computer 1" , intelCpu , seagate); 13 System.out.println(computer1.description()); 14 System.out.println("Computer 1 work:"); 15 computer1.work(); 16 //定义第二胎电脑的配件 17 CPU ADMCPU = new AMDCPU("AMD",1300,8); 18 Disk samsung = new SamsungDisk("Samsung",50,4096); 19 //组装第二台电脑 20 Computer computer2 = new Computer("computer 2" , ADMCPU , samsung); 21 System.out.println(computer2.description()); 22 System.out.println("Computer 2 work:"); 23 computer2.work(); 24 } 25 26 }
以上是我今日所写的一个小的系统,按照教学视频来做的,运用了一直以来学习的很多内容,使我好好地巩固了之前的知识,也让我更加了解项目的结构,需要注意的地方,在写项目之前需要画一个草图,去想想怎么去提取类中共有的元素,去写成一个父类,需要多个类实现的方法就去定义一个接口,去实现接口。
以上是关于学习打卡第五天的主要内容,如果未能解决你的问题,请参考以下文章