A small exercise in Java objects and classes, Java object exercises
I have been doing exercises in Eclipse. Is to do an exercise and execute one. I just learned about Java objects and classes. In practice, I put classes and execution in two. java files under the same package. Yes. (Get)
Related code:
1 public class Calc {2 // Its Attribute 3 int width = 90; 4 int height = 180; 5 String color = "green "; 6 7 // Method 8 int jia (int a, int B) {9 return a + B; 10} 11 12 int jian (int a, int B) {13 return a-B; 14} 15 16 void jieshao () {17 System. out. println ("I Am a calculator"); 18 System. out. println ("My length is:" + height); 19 System. out. println ("My width is:" + width); 20 System. out. println ("My color:" + color); 21} 22}
1 public class TestCalc {2 public static void main (String [] args) {3 Calc c = new Calc (); 4 5 c. jieshao (); 6 7 int a = 10; 8 int B = 20; 9 10 int result = c. jia (a, B); 11 12 int result2 = c. jian (a, B); 13 14 System. out. println ("addition result:" + result); 15 System. out. println ("subtraction Result:" + result2); 16} 17}
Note: During the call, the main function must re-assign values to the class (in the above example, Calc c =NewCalc ();)
I understand the rest.