Java对象与类中的一个小练习

来源:互联网 发布:飞剑问道好看吗 知乎 编辑:程序博客网 时间:2024/05/16 01:25

一直在Eclipse里做练习。是做一个练习,执行一个的那种。刚刚学习了Java的对象与类,练习中把类和执行放在同一包下的两个.java文件里面了。是可以执行的。(Get)

相关代码:

复制代码
 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 }
复制代码

注意:在调用的时候,main函数中要对类进行重新赋值(上例中的,Calc c=new Calc();)

其余部分,我都理解了。

原创粉丝点击