编写一个代表学生的Student类:
复制代码
1
2
3
4public class Student{ //1.成员变量 private String name; private int age;
复制代码
1
2
3//2构造器 //3.成员方法
复制代码
1
2
3
4//自我介绍,输出学生名字和年龄 public void intruction(){ System.out.println("我叫" + this.name + ",年龄" + this.age); }
复制代码
1
2
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21//set get方法 public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public void setAge(int age){ //setAge()方法对年龄的验证,如果年龄小于16岁,则输出“年龄太小了” if(age < 16){ System.out.println("年龄太小了"); } this.age = age; } public int getAge(){ return this.age; } }
复制代码
完成初始化,所有属性的值都由参数给定。
1
2
3
4public student(String name,int age){ this.name=name; this.age=age; }
最后
以上就是隐形睫毛膏最近收集整理的关于编写一个代表学生的Student类的全部内容,更多相关编写一个代表学生内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复