1.超市商品类
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=30000;
break;
default: price=150000;
}
return price;
}
public void inforshow(){
System.out.println("G00ds num="+getNum()+",name="+getName()+",price="+getPrice());
}
}
class GoodsShop{
private double money=0;
public double sellGoods(Goods car){
double price=car.getPrice();
return money=money+price;
}
public double getMoney(){
return money;
}
}
public class T4{
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);
}
}
运行结果
2.学生类
class student {
private String name;
private double score;
public student(){
}
public student(String name,double score){
this.name=name;
this.score=score;
System.out.println("姓名:"+name);
System.out.println("成绩:"+score);
}
public String getName(){
return name;
}
public double getScore() {
return score;
}
public void setName(String name){
this.name=name;
System.out.println("姓名:"+name);
}
public void setScore(double score){
this.score=score;
System.out.println("成绩:"+score);
}
}
public class T1 {
public static void main(String[] args){
student stu1=new student();
stu1.setName("lijun");
stu1.setScore(100);
student stu2=new student("lili",99);
}
}
运行结果
3.Father和child类
class Father {
private String name = "lijun";
class Child {
public void introFather()
{
System.out.println(name);
}
}
public void introFather() {
Child get = new Child();
get.introFather();
}
}
public class T1 {
public static void main(String[] args){
Father in=new Father();
in.introFather();
}
}
运行结果
以上题目主要是注意类,方法的创建和使用 。