Home >Java >javaTutorial >Detailed explanation of instances in Java objects and classes
I have been doing exercises in Eclipse. It’s the kind of doing an exercise and executing one. I just learned Java objects and classes. During the exercise, I put the class and execution in two .java files under the same package. is executable. (Get)
Related codes:
1 public class Calc { 2 // 其本属性 3 int width = 90; 4 int height = 180; 5 String color = "绿"; 6 7 // 方法 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("我是计算器");18 System.out.println("我的长度是:" + height);19 System.out.println("我的宽度是:" + width);20 System.out.println("我的颜色:" + 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("加法的结果:" + result);15 System.out.println("减法的结果是:" + result2);16 }17 }
Note: When calling, the class must be reassigned in the main function (in the above example, Calc c=new Calc();)
The rest Partly, I understand.
The above is the detailed content of Detailed explanation of instances in Java objects and classes. For more information, please follow other related articles on the PHP Chinese website!