封装继承

来源:互联网 发布:流星网络电视 卡 编辑:程序博客网 时间:2024/05/01 15:31

import java.text.*;
import java.util.*;
public class T10
{
 public static void main(String [] args)
 {
  //定义导师、学生姓名 各两个
  Student s1=new Student("刘月");
  Techer t1=new Techer("刘欢");
  Student s2=new Student("吴莫愁","在一起");
  Techer t2=new Techer("庾澄庆");
  //判断导师与学生姓名 循环5次
  if (t1.getTname()=="刘欢"&&s1.getName()=="刘月")
  {
   for (int i=1;i<=5 ;i++ )
   {
    System.out.println(s1.singing("我和你"));
   }
  }
  //判断导师与学生姓名 成立循环10次
  if (t2.getTname()=="庾澄庆"&&s2.getName()=="吴莫愁")
  {
   for (int i=1;i<=10 ;i++ )
   {
    System.out.println(s2.singing("小鸡哔哔"));
   }
  }
  //判断导师与学生姓名 反之循环20次
  else
  {
   for (int i=1;i<=20 ;i++ )
   {
    System.out.println(s2.singing("小鸡小鸡"));
   }
  }
 
 
 }
}
class Techer
{
 //定义属性 tname(导师名)
 private String tname;
 //定义构造方法
 public Techer(){}
 public Techer(String tname)
 {
  this.tname=tname;
 }
 //定义set、get方法
 public void setTname(String tname)
 {
  this.tname=tname;
 }
 public String getTname()
 {
  return tname;
 }
   
}
class Student
{
 //定义属性name(学生名)song(歌名)
 private String name;
 private String song="也许明天";
 //定义构造方法
 public Student(){}
 public Student(String name)
 {
  this.name=name;
 
 }
 public Student(String name,String song)
 {
  this.name=name;
  this.song=song;
 }
 //分别定义set、get方法
 public void setName(String name)
 {
  this.name=name;
 }
 public String getName()
 {
  return name;
 }
 public void setSong(String song)
 {
  this.song=song;
 }
 public String getSong()
 {
  return song;
 }
 //方法 演唱歌曲
 public String singing(String song)
 {
  this.song=song;
  String str="姓名:"+name+"\t歌曲: "+song;
  return str;
 }
}
class Person
{
 //属性成员:姓名、年龄、城市
 static int count=0;//学号
 String name;
 int age;
 static String country;
 //构造方法
 public Person(){
 
 }
 public Person(String name,int age){
  this.name=name;
  this.age=age;
 }
 //信息方法
 public void info(){
  count++;
  System.out.println("学号:2014_计算机系_"+count+" 姓名:"+name+" 年龄:"+age+" 城市:"+country);
 }
}
public class Test2
{
 
 public static void main(String[] args){
  Person p1=new Person("张三",19);
  Person p2=new Person("李四",20);
  Person p3=new Person("王五",21);
  Person p4=new Person();
  Person.country="哈尔滨";
  p1.info();
  p2.info();
  p3.info();
  p4.info();
 }
}
class Person
{
 //属性成员:姓名、年龄、城市
 static int count=0;//学号
 String name;
 int age;
 static String country;
 //构造方法
 public Person(){
 
 }
 public Person(String name,int age){
  this.name=name;
  this.age=age;
 }
 //信息方法
 public void info(){
  count++;
  System.out.println("学号:2014_计算机系_"+count+" 姓名:"+name+" 年龄:"+age+" 城市:"+country);
 }
}
public class Test2
{
 
 public static void main(String[] args){
  Person p1=new Person("张三",19);
  Person p2=new Person("李四",20);
  Person p3=new Person("王五",21);
  Person p4=new Person();
  Person.country="哈尔滨";
  p1.info();
  p2.info();
  p3.info();
  p4.info();
 }
}
package BaoJia;
public class BaoJia {
 //定义商品名称shname
 private String shname;
 public String getShname() {
  return shname;
 }
 public void setShname(String shname) {
  this.shname = shname;
 }
 //定义成本价costprice
 private double costprice;
 public double getCostprice() {
  return costprice;
 }
 public void setCostprice(double costprice) {
  this.costprice = costprice;
 }
 //定义销售价saleprice
 private double saleprice;
 public double getSaleprice() {
  return saleprice;
 }
 public void setSaleprice(double saleprice) {
  this.saleprice = saleprice;
 }
 //定义损耗价lossprice
 private double lossprice;
 public double getLossprice() {
  return lossprice;
 }
 public void setLossprice(double lossprice) {
  this.lossprice = lossprice;
 }
 
 //定义构造方法
 public BaoJia(){}
 public BaoJia(String shname,double costprice,double saleprice,double lossprice)
 {
  this.shname=shname;
  this.costprice=costprice;
  this.saleprice=saleprice;
  this.lossprice=lossprice;
 }
 //定义一个报价方法:输出商品名称及各种价的数据值
 public void display()
 {
  YunFei yf=new YunFei();
  System.out.println("商品名称"+shname+"\t成本价"+costprice+"\t销售价"+saleprice+"\t损耗价"+lossprice+"\t运费"+yf.getFre());
 }
 //定义一个计算利润方法:利润=销售家-成本价-损耗价
 public double pro()
 {
  double pro=saleprice-costprice-lossprice;
  return pro;
 }
 
}
package BaoJia;
public class YunFei extends BaoJia {
 //定义成员运费 fre
  private double fre=1;
 public double getFre() {
  return fre;
 }
 public void setFre(double fre) {
  this.fre = fre;
 }
 //定义构造方法
 public YunFei(){}
 public YunFei(String shname,double costprice,double saleprice,double lossprice)
 {
  super(shname,costprice,saleprice,lossprice);
 }
 //重写父类计算利润的方法(利润=销售家-成本价-损耗价-运费)
 // 重写时,要求调用父类的计算利润方法,将返回值-运费
 public double pro()
 {
  double d=super.pro();
  d-=fre;
  return d;
 }
}


0 0
原创粉丝点击