为类中的私有成员变量提供对外访问(读和写)的方法
定义一个表示学生信息的Student 类,Student 类中定义表示学生姓名和成绩私有成员变量,并为Student 类对象提供对外读、写私有成员变量的方法。
例如设置对象的姓名为ZhanSan,成绩为98,输出结果为:The student ZhanSan's score is 98!
输出内容如下:
The student ZhanSan's score is 98!
关于java#成员变量#的问题,如何解决?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注 public class Student { private String name ; private int score ; public String getName() { return name; } public int getScore() { return score; } public void setName(String name) { this.name = name; } public void setScore(int score) { this.score = score; } public void show(){ System.out.println("The student "+name+"'s score is "+score+"!"); } public static void main(String[] args) { Student student=new Student(); student.setName("ZhanSan"); student.setScore(98); student.show(); } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 sqlserver执行存储过程报错
- ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
- ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
- ¥100 matlab2009 32位一直初始化
- ¥15 Expected type 'str | PathLike[str]…… bytes' instead
- ¥15 为什么在iis上部署网站,服务器可以访问,但是本地电脑访问不了
- ¥15 三极管电路求解,已知电阻电压和三级关放大倍数
- ¥15 ADS时域 连续相位观察方法
- ¥15 Opencv配置出错
- ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。