博客:http://blog.csdn.net/muyang_ren
请用封装和构造完成以下练习,无特殊声明处请将属性写成private
练习一:package lhy.java_1day; class student { private String name="梁惠涌"; private char sex='男'; private int age=18; private String dire="嵌入式"; public String getName() { return name; } public char getSex() { return sex; } public int getAge() { return age; } public String getDire() { return dire; } } public class Test_1 { public static void main(String[] args) { // TODO 自动生成的方法存根 student lhyStudent = new student(); System.out.println("name: "+lhyStudent.getName()); System.out.println("sex : "+lhyStudent.getSex()); System.out.println("age : "+lhyStudent.getAge()); System.out.println("dire: "+lhyStudent.getDire()); } }
练习二:
定义一个Tank类(坦克类),为其定义x(横坐标),y(纵坐标),dir(方向:值为上下左右),Type(类型)4个字段,其访问修饰符均为公有;package lhy.java_1day; class tank{ public int x; public int y; public char dir; public String type; public tank() { // TODO 自动生成的构造函数存根 this.x = 100; this.y = 200; this.dir = '下'; this.type = "重型坦克"; } public void ShowData(){ System.out.println("x = "+this.x); System.out.println("y = "+this.y); System.out.println("dir = "+this.dir); System.out.println("type = "+this.type); } } public class Test_2 { public static void main(String[] args) { tank tank_01 = new tank(); tank_01.ShowData(); } }
练习三:
1) 创建一个计算机类:Computerpackage lhy.java_1day; class Computer{ private String name; private String type; private Double size; public Computer(){ this.name = "小新"; this.type = "联想"; this.size = 15.6; } public void showData() { System.out.println("name : "+this.name); System.out.println("type : "+this.type); System.out.println("size : "+this.size); } } public class Test_3 { public static void main(String[] args) { Computer lianxiangComputer = new Computer(); lianxiangComputer.showData(); } }
package lhy.java_1day; class Dog{ private String name = "小黑"; private char sex = '公'; public void showData(){ System.out.println("name : "+this.name); System.out.println("sex : "+this.sex); } public void YaoRen(String str){ if (str == "男人") { System.out.println("str = "+str); System.out.println("汪汪"); }else if (str == "女人") { System.out.println("str = "+str); System.out.println("喔喔"); }else { System.out.println("str = "+str); System.out.println("哇哇"); } } } public class Test_4 { public static void main(String[] args) { Dog dog = new Dog(); dog.showData(); dog.YaoRen("男人"); dog.YaoRen("女人"); dog.YaoRen("小孩"); } }
练习五:
1) 创建一个老师类:Teacher4) 创建一个老师对象,调用其ShowData();
package lhy.java_1day; import java.util.Scanner; class Teacher{ private int id; private String name; private String sex; private int age; private Double height; public void setId(int id) { this.id = id; } public void setName(String name) { if (name.length()>1 ||(name.length() <4)) { this.name = name; }else { System.out.println("名字长度出错"); System.out.println("请输入: name"); setName(new Scanner(System.in).next()); } } public void setSex(String sex) { if (sex.equals("男") || sex.equals("女")) { this.sex = sex; }else { System.out.println("性别输入错误(男 or 女)"); setSex(new Scanner(System.in).next()); } } public void setAge(int age) { if (age>20&&age<55) { this.age = age; }else { System.out.println("年龄输入错误(20~55)"); System.out.println("请输入: 年龄"); setAge(new Scanner(System.in).nextInt()); } } public void setHeight(Double height) { if (height>1.5 && height<1.8) { this.height = height; }else { System.out.println("身高输入错误(1.5~1.8)"); System.out.println("请输入: 身高"); setHeight(new Scanner(System.in).nextDouble()); } } public void showData(){ System.out.println(" ID :"+this.id); System.out.println(" name :"+this.name); System.out.println(" sex :"+this.sex); System.out.println(" age :"+this.age); System.out.println("height :"+this.height); } } public class Test_5 { public static void main(String[] args) { Teacher teacher = new Teacher(); Scanner stdin = new Scanner(System.in); System.out.println("请输入:ID"); teacher.setId(stdin.nextInt()); System.out.println("请输入: name"); teacher.setName(stdin.next()); System.out.println("请输入: 年龄"); teacher.setAge(stdin.nextInt()); System.out.println("请输入: 性别"); teacher.setSex(stdin.next()); System.out.println("请输入: 身高"); teacher.setHeight(stdin.nextDouble()); stdin.close(); teacher.showData(); } }
练习六:
1) 定义一个水果类:Fruit,内有公有的字段:name,color,weiDao14) 调用该榨汁机对象的GetFruitZhi(Fruit f)方法,打印结果为:一杯黄色的桔子汁,味道甜甜的
package lhy.java_1day; class Fruit{ public String name; public String color; public String weidao; public Fruit(String name,String color,String weidao){ this.name=name; this.color=color; this.weidao=weidao; } public Fruit(){ } public String GetMsg(){ return this.name+","+this.color+","+this.weidao; } } class ZhaFruit{ public String GetFruitZhi(Fruit f){ String string ="一杯"+f.color+"的"+f.name+"汁,味道"+f.weidao; return string; } } public class Test_6 { private static String msg; public static void main(String[] args) { Fruit fruit1 = new Fruit("苹果","青色","酸酸的"); msg = fruit1.GetMsg(); System.out.println(msg); ZhaFruit zhafuit1 =new ZhaFruit(); System.out.println(zhafuit1.GetFruitZhi(fruit1)); Fruit fruit2 = new Fruit("桔子","黄色","甜甜的"); ZhaFruit zhafuit2 =new ZhaFruit(); System.out.println(zhafuit2.GetFruitZhi(fruit2)); } }
原文地址:http://blog.csdn.net/muyang_ren/article/details/46389487