Java basics-exercises about interfaces and abstract classes and java
Well, you must understand it and then try it yourself !!! These questions give me a better understanding of interfaces and abstract classes.
1. Design a commodity field:
Product Name, weight, price, accessory quantity, accessory manufacturer (array, because there may be multiple manufacturers)
Requirement: Constructor
Override toString Method
Override the equals method to compare two items
1 package TT; 2 3 import java. util. arrays; 4 5 public class G {6 private int weight; 7 private String commodity name; 8 private int price; 9 private int accessory quantity; 10 private String [] accessory manufacturer list; 11 12 public G (int weight, String product name, int price, int accessory quantity, String [] accessory manufacturer list) {13 super (); 14 this. weight = weight; 15 this. product Name = product name; 16 this. price = price; 17 this. accessory quantity = accessory quantity; 18 this. accessory manufacturer list = accessory manufacturer list; 19} 20 21 @ Override22 public String toString () {23 return "G [Weight =" + weight + ", product name =" + product name + ", price =" + Price + ", accessory quantity = "24 + accessory quantity +", accessory manufacturer list = "+ Arrays. toString (accessory manufacturer list) + "]"; 25} 26 27 public boolean equals (Object obj) {28 if (this = obj) {29 return true; 30} 31 32 if (obj instanceof G) {33G goods = (G) obj; 34 35 if (this. weight = goods. weight & this. price = goods. price 36 & this. product Name. equals (goods. product Name) & this. accessory quantity = goods. accessory quantity) {37 for (int I = 0; I <this. Accessory manufacturer list. length; I ++) {38 if (this. Accessory manufacturer list [I]! = Goods. accessory manufacturer list [I]) {39 return false; 40} 41} 42 return true; 43} else {44 return false; 45} 46} else {47 return false; 48} 49} 50 // put the test class together with the above 51 public static void main (String [] args) {52 53 String [] Vendor List 1 = {"Microsoft", "Sony", "Samsung"}; 54 String [] Vendor List 2 = {"Microsoft ", "Sony", "Samsung"}; 55G g1 = new G (40, "computer", 4000, 30, Vendor List 1); 56G g2 = new G (40, "display", 4000, 30, Vendor List 2); 57 58 System. out. println (g1); 59 System. out. println (g2); 60 61 System. out. println (g1.equals (g2); 62} 63}
2. design an abstract class and demonstrate its usage (level 3)
1 package TT; 2 3 // abstract class 4 abstract class mathematical instructor standard {5 abstract void class Mathematics (); 6 abstract int add (int a, int B ); 7} 8 9 // Level 2 abstract class 10 abstract class general math instructor standard extends Math instructor standard {11 void lecture math () {12 System. out. println ("I can talk about mathematics"); 13} 14} 15 16 // Level 3 abstract class 17 true math instructor extends general math instructor standard {// Note whether abstract modification can be used here, modified will affect the upper and lower 18 // you can rewrite, or do not rewrite 19 void to speak mathematics () {20 System. out. println ("I can speak higher"); 21} 22 int add (int a, int B) {23 return a + B; 24} 25} 26 class Test {27 public static void main (String [] args) {28 Math instructor standard t1 = new real math instructor (); 29 int result = t1.add (44,444 4); 30 System. out. println ("Calculation result:" + result); 31 t1. speaking of Mathematics (); 32} 33}
3. Design an interface and an implementation class to implement this interface
1 package TT; 2 3 interface banking standard {4 int MONEY = 9900000; 5 void withdrawal (); 6 void deposit (); 7} 8 9 class Industrial and Commercial Bank implements banking standard {10 public void () {11 System. out. println ("money goes into the Bank"); 12} 13 14 @ Override // all methods are used 15 public void deposit () {16 System. out. println ("money goes out"); 17} 18} 19 public class Test {20 public static void main (String [] args) {21 bank Standard bank = new Industrial and Commercial bank (); 22 bank. withdraw money (); 23 bank. save money (); 24 25 System. out. println ("the old bank is" + Bank standards. MONEY); // The bank Standard used here will report an error, The static field bank Standard. MONEY shoshould be accessed in a static way26 27} 28 29}
4. Design a class to implement multiple interfaces at the same time
1 package TT; 2 3 interface construction standard {4 void kaimen (); 5 6 void guanmen (); 7} 8 9 interface banking standard {10 int MONEY = 9999; 11 12 void withdrawal (); 13 14 void deposit (); 15} 16 17 class Industrial and Commercial Bank implements bank Standard, building standard {18 19 @ Override20 public void kaimen () {21 System. out. println ("I can open the door"); 22 23} 24 25 @ Override26 public void guanmen () {27 // TODO Auto-generated method stub28 System. out. println ("I can close the door"); 29 30} 31 32 @ Override33 public void () {34 // TODO Auto-generated method stub35 System. out. println ("I can get the money"); 36} 37 38 @ Override39 public void save () {40 // TODO Auto-generated method stub41 System. out. println ("I can save money"); 42 43} 44 45} 46 47 public class Test2 {48 public static void main (String [] args) {49 ICBC bank = new ICBC (); 50 bank. withdrawal (); 51 bank. save money (); 52 System. out. println ("the bottom of the bank is" + ICBC. MONEY); 53 54 bank. kaimen (); 55 bank. guanmen (); 56} 57}
5. Design a class to implement its two interfaces while inheriting a class (specifically, what interfaces are self-designed)
1 package TT; 2 interface construction standard {3 void kaimen (); 4 void guamen (); 5} 6 interface banking Standard 7 {8 int MONEY = 900000; // The variables in the interface are equivalent to 9 void () of the public static final type; 10 void (); 11} 12 13 14 class financial institution {15 void currency circulation () {16 System. out. println ("currency circulation"); 17} 18} 19 class Industrial and Commercial Bank of China extends financial institution implements bank Standard, construction standard {20 21 public void () {22 System. out. println ("money goes into the Bank"); 23} 24 25 26 public void deposit () {27 System. out. println ("money from a bank in your hands"); 28} 29 30 31 @ Override32 public void kaimen () {33 System. out. println ("a bank has a door to open"); 34 35} 36 37 38 @ Override39 public void guamen () {40 System. out. println ("the bank has an iron gate, and no one will be able to get in after it is closed"); 41 42} 43 44} 45 46 class Test4 {47 public static void main (String [] args) {48 ICBC bank = new ICBC (); 49 bank. get (); 50 bank. save money (); 51 System. out. println ("the bottom of the bank is" + bank. MONEY); 52 53 bank. kaimen (); 54 bank. guamen (); 55 56 bank. currency circulation (); 57 58} 59}
The last egg !!!!
As I put these examples in the same TT package during my exercises, there are five files in total. Finally, The last three examples above start to report an error (for example, The error: The type bank Standard is already defined ).
Ah, ah, debugging has not been completed for half an hour. Later, I thought for a moment, since Eclipse prompts that XXX has been defined, isn't it necessary to repeatedly define the same class name file no matter how many classes you have created under the same package? And then start to Delete two). Sure enough, the operation is successful. After careful consideration, since the public classes can be accessed under the same package, such consistency verification can save a lot of resources.