博客:http://blog.csdn.net/muyang_ren
请用封装和构造完成以下练习,无特殊声明处请将属性写成private
练习一:
1) 创建一个学生类
2) 创建四个私有的字段:name,sex,age,dire
3) 分别为私有的字段提供属性
4) 测试:创建学生对象,设置属性,再分别在控制台打印该学生的基本信息
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个字段,其访问修饰符均为公有;
为Tank类定义一个构造方法实现4个字段的初始化
为Tank类定义一个ShowData方法,控制台打印4个字段值
创建一个Tank对象,横坐标为10,纵坐标为50,方向为右,类型为重型坦克,并通过调用对象的ShowData()方法打印Tank信息
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) 创建一个计算机类:Computer
2) 创建三个字段:name:计算机名,type:计算机品牌,size:尺寸
3) 创建一个构造函数为三个字段赋初始值
4) 添加一个ShowData方法打印各字段值
5) 创建一个计算机对象,调用其ShowData();
package 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(); } }
练习四:
1) 创建一个狗类:Dog
2) 为狗类添加一个name字段和sex字段
3) 为name字段和sex字段分别提供属性(name:无校验,sex:只能是”公和母”)
4) 为Dog添加ShowData方法,打印名字和性别
5) 为Dog添加YaRen方法,实现狗咬人功能,YaoRen(string str),如果str是一个”男人”,输出“汪汪”,如果str是一个”女人”,输出“喔喔”,如果str是其它的输出”哇哇”
6) 测试
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) 创建一个老师类:Teacher
2) 为老师添加字段:id,name,sex,age,height
3) 分别为字段添加对应的属性:
id:int,不用校验
name:string,长度大于一位少于四位
sex:string,只能是男和女
age:int,只能在20-55之间
height:double,只能在1.50-1.80之间
定义一个ShowData方法,打印编号、姓名、性别、身高
4) 创建一个老师对象,调用其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,weiDao
2) 定义一个构造函数:为三个字段赋初始值
3) 为Fruit类添加一个方法public string GetMsg(),返回该水果的基本信息
4) 定义一个榨汁机类:ZhaFruit
5) 为ZhaFruit类添加一个榨水果汁方法:public string GetFruitZhi(Fruit f),返回:
”一杯XX(颜色)的XX(水果名)汁,味道XXX”
6) 在测试类Program的Main方法内创建一个水果:
名字:苹果,颜色:青色,味道:酸酸的
7) 调用该水果的GetMsg(),用一个字符串变量接收
8) 打印该方法的返回值:”苹果,青色,酸酸的”
9) 创建一个ZhaFruit榨汁机对象
10) 调用该榨汁机对象的string GetFruitZhi(Fruit f)方法,并传一个水果(苹果)对象
11) 调用该榨汁机对象的GetFruitZhi(Fruit f)方法,打印结果为:一杯青色的苹果汁,味道酸酸的
12) 定义一个水果对象:
名字:桔子,颜色:黄色,味道:甜甜的
13) 调用该榨汁机对象的string GetFruitZhi(Fruit f)方法,并传一个水果(桔子)对象
14)
调用该榨汁机对象的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)); } }