[java]一个简单的程序巩固基本概念

    由一下的例子,我们可以对 成员变量,类变量,构造函数,方法,对私有变量的取值和赋值,可窥一斑。

  1. public class Test {
  2.     //成员变量
  3.     private int x; //注意这里的属性
  4.     private int y;
  5.     private String id;
  6.     //类变量
  7.     static int count;
  8.     
  9.     //构造函数
  10.     public Test() {
  11.         id = "Test" + (++count);
  12.     }
  13.     
  14.     public Test(int x,int y) {
  15.         this();
  16.         this.x = x;
  17.         this.y = y;
  18.     }
  19.     
  20.     //方法
  21.     public void showTest() {
  22.         System.out.println(id + "/n" + "    x = " + x + "/n" + "    y = " + y);
  23.     }
  24.     
  25.     //增加以后在多个类中的对Private属性变量的操作演示
  26.     public String getId() {
  27.         return id;
  28.     }
  29.     
  30.     public void setId(String id) {
  31.         this.id = id;
  32.     }
  33.     
  34.     public void setX(int x) {
  35.         this.x = x;
  36.     }
  37.     
  38.     
  39.     //为了演示,这里就放在同一个类里面
  40.     public static void main(String[] args) {
  41.     
  42.         Test exam = new Test();
  43.         exam.showTest();
  44.         
  45.         //exam.x = 5;
  46.         //exam.showTest();
  47.         
  48.         exam.setId("Example");
  49.         exam.setX(8);
  50.         System.out.println(exam.getId());   
  51.         exam.showTest();
  52.     }
  53. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值