java 非法的表达式_java非法的表达式开始

本文探讨了如何将Java代码中的变量和方法从`private`、`protected`和`public`调整为合适的访问级别,并通过创建`display()`方法实现了主函数的模块化。实例展示了如何在`Main`类中正确地组织不同访问权限的成员变量,提高代码可读性和维护性。
摘要由CSDN通过智能技术生成

public class Main {

public static void main(String[] args) {

/**

* 这里面不应该有private这种限定符

*/

private int a = 1;

int b = 2;

protected int c = 3;

public int d = 4;

System.out.println(a + " " + b + " " + c + " " + d);

}

}

改写如下public class Main {

private int a = 1;

int b = 2;

protected int c = 3;

public int d = 4;

public static void main(String[] args) {

Main m = new Main();

m.display();

}

private void display() {

System.out.println(a + " " + b + " " + c + " " + d);

}

}

或者public class Main {

private static int a = 1;

static int b = 2;

static protected int c = 3;

static public int d = 4;

public static void main(String[] args) {

System.out.println(a + " " + b + " " + c + " " + d);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值