用超市进销存管理系统解释:构造方法,封装,this关键字和子类父类
5.3.4——用构造方法初始化成员变量
class Triangle{
int x,y,z;
public Triangle(int i,int j,int k) {
x=i;y=j;z=k;
}
public static boolean judge(Triangle m) {
if(Math.sqrt(m.x*m.x+m.y*m.y)==Math.sqrt(m.z*m.z))
//引用math类库的sqrt()方法
return true;
else
return false;
}
public static void main(String[] args) {
Triangle t1=new Triangle(3,4,5); //实例化对象t1,调用构造方法对其进行初始化
if(judge(t1)) //调用judge()方法,判断t1的成员变量是
//否能构成直角三角形的3条边长
System.out.println("这是一个直角三角形");
else
System.out.println("这不是一个直角三角形");
}
}
运行结果
这是一个直角三角形
5.3.5——构造方法重载
class Time1
{
private int hour; //0-23
private int minute; //0-59
private int second; //0-59
public Time1() {
setTime(0,0,0);
}
public Time1 (int hh) {
setTime(hh,0,0);
}
public Time1 (int hh,int mm) {
setTime(hh,mm,0);
}public Time1 (int hh,int mm,int ss) {
setTime(hh,mm,ss);
}
public void setTime(int hh,int mm,int ss) {
hour=((hh>=0&&hh<24)?hh:0);
minute=((mm>=0&&mm<60)?mm:0);
second=((ss>=0&&ss<60)?ss:0);
}
public String toString() {
return(hour+":"+(minute<10?"0":"")+minute+":"+
(second<10?"0":"")+second);
}
}
public class MyTime1 {
private static Time1 t0,t1,t2,t3;
public static void main(String[] args) {
t0=new Time1();
t1=new Time1(11);
t2=new Time1(22, 22);
t3=new Time1(11, 22, 33);
System.out.println("t0= "+t0.toString());
System.out.println("t1= "+t1.toString());
System.out.println("t2= "+t2.toString());
System.out.println("t3= "+t3.toString());
}
}
运行结果
t0= 0:00:00
t1= 11:00:00
t2= 22:22:00
t3= 11:22:33
5.5——封装类
1.创建类Girl.java
public class Girl {
String name;
int age;
boolean flag;
public Girl(){}
public Girl(String name,int age,boolean flag) {
this.name=name;
this.age=age;
this.flag=flag;}
public void show() {
System.out.println("我是"+name+",今年"+age+"岁了,目前"+(flag?"入住":"没入住"));}
}
运行结果
无输出
2.创建测试类TestGirl.java
public class TestGirl {
public static void main(String[] args) {
Girl g=new Girl();
g.show();
Girl g1=new Girl("小红",-18,true);
g1.show();
}
}
我是null,今年0岁了,目前没入住
我是小红,今年-18岁了,目前入住
3.封装类 Girl.java
public class Girl {
private String name;
private int age;
private boolean flag;
public Girl(){}
public Girl(String name,int age,boolean flag) {
this.name=name;
this.age=age;
this.flag=flag;}
public void show() {
System.out.println("我是"+name+",今年"+age+"岁了,目前"+(flag?"入住":"没入住"));}
//2.提供公共的get和set方法,并且在方法体中进行合理的判断
public String getName() {return name;}
public void setName(String name) {this.name=name;}
public void setAge(int age) {
if(age>0&&age<=18) {
this.age=age;}
else {System.out.println("年龄不合理");}
}
public boolean getFlag() { return flag; }
public void setFlag() { this.flag=flag;}
}
4.需要在构造方法里调用set成员变量进行合理的判断
public class TestGirl {
public static void main(String[] args) {
Girl g1=new Girl("小红",-18,true);
g1.setAge(19);
}
}
运行结果
年龄不合理
5.6关键字this的用法
关键字this代表一个引用,指向正在调用该方法的当前对象
程序1
package unit5;
public class TestThis {
public TestThis() {
System.out.println(this);
}
public static void main(String[] args) {
TestThis tt=new TestThis();
}
}
运行结果
unit5.TestThis@58ceff1
意思是在unit5包中的TestThis类目前对象this是内存中一个地址
程序2
package unit5;
public class TestThis {
public TestThis() {
System.out.println(this);
}
void show() {
System.out.println(this);
}
public static void main(String[] args) {
TestThis tt=new TestThis();
tt.show();
System.out.println(tt);
}
}
运行结果
unit5.TestThis@58ceff1
unit5.TestThis@58ceff1
unit5.TestThis@58ceff1
发现this和tt是指向同一地址
程序3
public class TestThis {
String name;int age;
TestThis(){System.out.println(this);}
TestThis(String name,int age){this.name=name;this.age=age;}
void show() {
System.out.println(this);
}
void show1() {
System.out.println(name+" "+age);
}
public static void main(String[] args) {
TestThis tt=new TestThis();
tt.show();
System.out.println(tt);
TestThis tt1=new TestThis("王林",18);
tt1.show1();
}
}
运行结果
unit5.TestThis@58ceff1
unit5.TestThis@58ceff1
unit5.TestThis@58ceff1
王林 18
5.8 项目实训:构建超市的商品类并打印商品信息
1.建立Goods类
public class Goods {
private int num;//编号
private String name;//型号
private double price;//单价
Goods(){super();}
public Goods(int num,String name) {
super(); this.num=num;this.name=name; }
public int getNum() {return num;}
public void setNum(int num) {this.num=num;}
public String getName() {return name;}
public void setName(String name) {this.name=name;}
public double getPrice() {
int NAME=getNum();
switch(NAME) {
case 001:price=20000;break;
case 002:price=300000;break;
default:price=150000;
}
return price;
}
public void inforShow() {
System.out.println("Goods num="+getNum()+",name="+getName()+",price="+getPrice());
}
}
2.建立GoodsShop类
public class GoodsShop {
private double money=0;//卖出商品的收入
public double sellGoods(Goods car) {//卖商品的行为方法,返回收入
double price=car.getPrice();
return money=money+price;}
public double getmoney() {
return money;
}
}
3.建立TestGoodShop类
public class TestGoodShop {
public static void main(String[] args) {
GoodsShop goodsShop =new GoodsShop();
Goods g1=new Goods(001,"可口可乐");
goodsShop.sellGoods(g1);
Goods g2=new Goods(002,"雪碧");
goodsShop.sellGoods(g2);
g1.inforShow();
g2.inforShow();
double counMoney=goodsShop.getmoney();
System.out.println("商品出售的价格是:"+counMoney);
}
}
运行结果
Goods num=1,name=可口可乐,price=20000.0
Goods num=2,name=雪碧,price=300000.0
商品出售的价格是:320000.0
P82 习题——编程题
1.按照要求设计一个学生类Student,并进行测试。
public class Student {
private String name;
private int score;
public String getName(){ //获取名字
return name;
}
public int getScore(){ //获取成绩
return score;
}
public Student() {} //构造方法(无参)
public Student(String name, int score) { //构造方法(有参)
this.name = name;
this.score = score;
}
}
public class TestStudent {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student("温景然", 60);
System.out.println(stu1.getName() + " " + stu1.getScore());
System.out.println(stu2.getName() + " " + stu2.getScore());
}
}
运行结果
null 0
温景然 60
2.定义一个Father和Child类,并进行测试。
public class Father {
private String name="zhangjun";
class Child{
public void introFather() {
System.out.println(name);
}
}
}
public class TestFather {
public static void main(String[] args) {
Father.Child c1=new Father().new Child();
c1.introFather();
}
}
运行结果
zhangjun
ull 0温景然 60
2.定义一个Father和Child类,并进行测试。
public class Father {
private String name="zhangjun";
class Child{
public void introFather() {
System.out.println(name);
}
}
}
public class TestFather {
public static void main(String[] args) {
Father.Child c1=new Father().new Child();
c1.introFather();
}
}
运行结果
zhangjun