面向对象 程序题"/>
面向对象 程序题
第一题:
1、student类
package Day05;
public class Student {
private String id;//学生的id
private String name;//学生的姓名
private String sex;//学生的性别
private int age;//学生的年龄
/*构造器*/
public Student(String id, String name, String sex, int age){
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
/*getter()和setter()方法**/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2、Test01类
package Day05;
public class Test01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student("01", "tom", "男", 17);
System.out.println(s.getAge());
System.out.println(s.getId());
System.out.println(s.getName());
System.out.println(s.getSex());
}
}
第二题:
1、WuMingFen类
package Day05;
public class WuMingFen {
private String theMa;//面码
private int quantity;//粉的分量
private boolean likeSoupe;//是否带汤
//构造函数
public WuMingFen(String theMa, int quantity, boolean likeSoupe){
this.theMa = theMa;
this.quantity = quantity;
this.likeSoupe = likeSoupe;
}
//重载构造函数1
public WuMingFen(String theMa, int quantity){
this.theMa = theMa;
this.quantity = quantity;
}
//重载构造函数2
public WuMingFen(){
}
//getter()和setter()方法
public String getTheMa() {
return theMa;
}
public void setTheMa(String theMa) {
this.theMa = theMa;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean isLikeSoupe() {
return likeSoupe;
}
public void setLikeSoupe(boolean likeSoupe) {
this.likeSoupe = likeSoupe;
}
//打印信息
public void check(){
//System.out.println(getTheMa());
//System.out.println(getQuantity());
//System.out.println(isLikeSoupe());
//优化
System.out.println("面码:" + theMa +
"粉的质量:" + quantity +
"是否带汤:" + likeSoupe);
}
}
2、测试类
package Day05;
public class Test02 {
public static void main(String[] args) {
WuMingFen f1 = new WuMingFen("牛肉", 3, true);
WuMingFen f2 = new WuMingFen("牛肉", 2);
WuMingFen f3 = new WuMingFen();
f3.setTheMa("酸辣面");
f3.setQuantity(2);
f3.setLikeSoupe(true);
f1.check();
f2.check();
f3.check();
}
}
第四题:
1、StaticDemo类
public class StaticDemo {
private static int a = 10;
private int b = 5;
public void print(){
System.out.println("a=" + a + "b=" + b);
}
}
2、测试类
public static void main(String[] args) {
StaticDemo m = new StaticDemo();
m.print();
}
第五、三题:
1、MyTime类:
package Day05;
public class MyTime {
private int hour;//时
private int minute;//分
private int second;//秒
//构造方法
public MyTime(int hour, int minute, int second){
this.hour = hour;
this.minute = minute;
this.second = second;
}
//getter()和setter()方法
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
//display方法,输出时间
public void display(){
System.out.println(hour + "小时" + minute + "分钟" + second + "秒");
}
}
2、测试类:
public static void main(String[] args) {
MyTime m = new MyTime(5, 24, 45);
m.display();
}
更多推荐
面向对象 程序题
发布评论