作业6
来源:互联网 发布:java编程思想第六版 编辑:程序博客网 时间:2024/05/22 15:38
/*编写一个学生类Students,该类成员变量包括学号no、姓名name、性别sex和年龄age,该类的成员方法有genNo()getName ).
* getSex().getAge().和setAge() 添加构造方法为所有成员变量赋初值,构造方法要有4种格式:
(1) 包括no,name,sex 和age四个参数。
(2) 包括 no.name 和sex 三个参数。
(3) 包括mo和name两个参数。
(4) 只包括no一个参数。
*/
class Student {// 创建Student这个类
private int no;// 成员变量
private static int age;
private String name;
private String sex;
public void setAge(int myAge) {//封装
if (myAge <= 0) {
System.out.println("年龄出错,使用默认年龄18岁代替");
age=18;
}
else
age=myAge;
}
public int getNo() {
System.out.println("学号:"+no);
return no;
}
public String getName(){
System.out.println("姓名:"+name);
return name;}
public String getSex(){
System.out.println("性别:"+sex);
return sex;}
public int getAge() {
System.out.println("年龄:"+age);
return age;
}
public Student(int myNo, String myName, String mySex, int myAge) {//构造方法变量初始化
no = myNo;
name = myName;
sex = mySex;
age = myAge;
}
public Student(int myNo, String myName, String mySex) {
no = myNo;
name = myName;
sex = mySex;
}
public Student(int myNo, String myName) {
no = myNo;
name = myName;
}
public Student(int myNo) {
no = myNo;
}
public static void main(String[] args) { // 声明对象和创建对象
Student Student1= new Student(1625,"张三","m",20);
Student Student2= new Student(1624,"李四","f");
Student Student3= new Student(1623,"张三");
Student Student4= new Student(1622);
Student1.setAge(age);
Student1.getNo();
Student1.getName();
Student1.getSex();
Student1.getAge();
Student2.getNo();
Student2.getName();
Student2.getSex();
Student3.getNo();
Student3.getName();
Student4.getNo();
}
}
* getSex().getAge().和setAge() 添加构造方法为所有成员变量赋初值,构造方法要有4种格式:
(1) 包括no,name,sex 和age四个参数。
(2) 包括 no.name 和sex 三个参数。
(3) 包括mo和name两个参数。
(4) 只包括no一个参数。
*/
class Student {// 创建Student这个类
private int no;// 成员变量
private static int age;
private String name;
private String sex;
public void setAge(int myAge) {//封装
if (myAge <= 0) {
System.out.println("年龄出错,使用默认年龄18岁代替");
age=18;
}
else
age=myAge;
}
public int getNo() {
System.out.println("学号:"+no);
return no;
}
public String getName(){
System.out.println("姓名:"+name);
return name;}
public String getSex(){
System.out.println("性别:"+sex);
return sex;}
public int getAge() {
System.out.println("年龄:"+age);
return age;
}
public Student(int myNo, String myName, String mySex, int myAge) {//构造方法变量初始化
no = myNo;
name = myName;
sex = mySex;
age = myAge;
}
public Student(int myNo, String myName, String mySex) {
no = myNo;
name = myName;
sex = mySex;
}
public Student(int myNo, String myName) {
no = myNo;
name = myName;
}
public Student(int myNo) {
no = myNo;
}
public static void main(String[] args) { // 声明对象和创建对象
Student Student1= new Student(1625,"张三","m",20);
Student Student2= new Student(1624,"李四","f");
Student Student3= new Student(1623,"张三");
Student Student4= new Student(1622);
Student1.setAge(age);
Student1.getNo();
Student1.getName();
Student1.getSex();
Student1.getAge();
Student2.getNo();
Student2.getName();
Student2.getSex();
Student3.getNo();
Student3.getName();
Student4.getNo();
}
}
阅读全文
0 0
- 作业:C++作业6
- 作业6
- 作业6
- 作业6
- 作业6
- 作业6
- 作业6
- 作业6
- 作业-6
- 作业6
- 作业6
- 作业6
- 作业6
- 作业6
- 作业6
- 第6 次作业
- 现代教育技术课后作业(6)
- c#实验6作业
- train_test_split用法
- 阿里服务器上安装mysql,外网无法连接问题
- Print格式化输出
- vue脚手架使用及问题
- Windows线程池
- 作业6
- Java中的值传递与“引用传递”
- msys64+mingw环境搭建
- 51nod 1596 搬货物 2的倍数的运算+优先队列处理
- iPhone X适配 最简单粗暴的
- ubuntu下wps不能输入中文
- Unity 构建IOS和ANDROID工程 (二)
- 数据结构和算法学习02-时间复杂度和空间复杂度
- wordpress 处理 ajax 请求