Java oop的題2(重要5題):靜態(tài)屬性銷售,動(dòng)物類,電子產(chǎn)品類,抽象類人,系統(tǒng)

創(chuàng)建商品類,包含商品名稱,價(jià)格屬性,包含靜態(tài)屬性銷售總價(jià)格,實(shí)例?化三個(gè)商品對(duì)象,統(tǒng)計(jì)商品的總價(jià)格
package a;
class shangpin {
public static void main(String[] args) {
// class sp在class shangpin里面
sp s1 = new sp();
s1.ID = "1";
s1.price = 3000;
s1.name = "手機(jī)";
// 靜態(tài)變量可以使用類名直接進(jìn)行調(diào)用
sp s2 = new sp();
s2.ID = "2";
s2.price = 4000;
s2.name = "電腦";
// 靜態(tài)變量可以使用類名直接進(jìn)行調(diào)用
// 生命周期是程序加載到程序結(jié)束
// 可以被多個(gè)對(duì)象所共享
sp s3 = new sp();
s3.ID = "2";
s3.price = 2;
s3.name = "辣條";
sp.zongjiage = s1.price + s2.price + s3.price;
System.out.println(sp.zongjiage+"元。");
}
}
class sp {// class sp在class shangpin外面
public String ID; // 普通成員變量
public String name; // 普通成員變量
public double price;// 普通成員變量價(jià)格
// 統(tǒng)計(jì)商品的總價(jià)格
public static double zongjiage = 0; // 聲明靜態(tài)變量zongjiage
}

2.聲明一個(gè)動(dòng)物類,包含動(dòng)物名稱屬性,聲明動(dòng)物叫的方法,聲明犬類繼承?動(dòng)物類,重寫叫的方法,打印犬類是汪汪的叫,聲明貓類繼承動(dòng)物類,重寫?叫的方法,打印貓類是喵喵的叫
package a;
public class animal {
public static void main(String[] args) {
dongwu c = new cat("黑貓");
c.sayc();
dongwu d = new? dog("白狗");
d.sayd();
}
}
class dongwu{
public String name;
public dongwu(String name) {
this.name = name;
}
public void sayc() {
System.out.println(this.name + "是喵喵的叫");
}
public void sayd() {
System.out.println(this.name + "是汪汪的叫");
}
}
class dog extends dongwu {
public dog(String name) {
super(name);
}
}
class cat extends dongwu {
public cat(String name) {
super(name);
}
}

3.聲明一個(gè)電子產(chǎn)品類,包含功能的方法,聲明電腦類繼承自電子產(chǎn)品類,?重寫功能的方法,聲明手機(jī)類繼承電子產(chǎn)品類,重寫功能的方法,聲明MP3繼?承電子產(chǎn)品類,重寫功能的方法,在主類里再聲明一個(gè)判斷的方法,根據(jù)傳?入電子設(shè)備的不同打印不同的功能
package a;
public class demo {
public static void main(String[] args) {
DianZiChanPin? P = new? MP3("蘋果電腦");
P.gongnengd();
DianZiChanPin? S = new? diannao("華為手機(jī)");
S.gongnengs();
DianZiChanPin M = new? diannao("詩書畫唱MP3");
M.gongnengM();
}
}
class DianZiChanPin {
public String name;
public DianZiChanPin (String name) {
this.name = name;
}
public void gongnengd() {
System.out.println(this.name + "的功能是看釘釘");
}
public void gongnengs() {
System.out.println(this.name + "的功能是打電話");
}
public void gongnengM() {
System.out.println(this.name + "的功能是聽歌");
}
}
class diannao extends DianZiChanPin? {
public diannao(String name) {
super(name);
}
}
class shouji extends DianZiChanPin? {
public shouji(String name) {
super(name);
}
}
class MP3 extends DianZiChanPin? {
public? MP3(String name) {
super(name);
}
}

按要改名的類,再按F2就可以換類的名。
4.聲明抽象類人,包含編號(hào),姓名,年齡屬性,包含抽象方法吃飯,學(xué)習(xí),?聲明接口購物,聲明接口上課,聲明接口做備課,聲明接口做作業(yè),聲明老?師類繼承自抽象類人類實(shí)現(xiàn)購物和備課的接口,聲明學(xué)生類繼承抽象類人類?實(shí)現(xiàn)寫作業(yè)和上課的接口,在main函數(shù)中進(jìn)行調(diào)用
package b;
public class demo {
public static void main(String[] args) {
ren t = new teacher();// /向上轉(zhuǎn)型,抽象類
gouwu t1 = new teacher();// 向上轉(zhuǎn)型,接口
beike t2 = new teacher();
t.name = "詩書";
t.chifan();
t1.maidx();
t2.bk();
ren s = new student();
shangke s1 = new student();
zuozuoye s2 = new student();
s.name = "畫唱";
s.xuexi();
s1.sk();
s2.zzy();
}
}
abstract class ren {
String id;
String name;
int age;
public abstract void chifan();
public abstract void xuexi();
// 抽象類不能包含學(xué)習(xí),因?yàn)樗腥瞬灰欢ǘ紝W(xué)習(xí),題目有錯(cuò),這里按題目要求來,課程聲明為接口
}
interface gouwu {
public abstract void maidx();
}
interface shangke {
public abstract void sk();
}
interface beike {
public abstract void bk();
}
interface zuozuoye {
public abstract void zzy();
}
class teacher extends ren implements gouwu, beike {
public void chifan() {
System.out.println(this.name + "在吃飯");
}
public void bk() {
System.out.println("在備課");
}
public void maidx() {
System.out.println("在買東西");
}
public void xuexi() {
}
}
class student extends ren implements zuozuoye, shangke {
public void xuexi() {
System.out.println(this.name + "在學(xué)習(xí)");
}
public void sk() {
System.out.println("在上課");
}
public void zzy() {
System.out.println("在做作業(yè)");
}
public void chifan() {
}
}
5.完善租車系統(tǒng)
package b;
import java.util.Scanner;
public class zuche{
public static void main(String[] args) {
che xiaohuoche=new huoche(1,"小貨車",30, 0, 3);
che dahuoche=new huoche(2,"大貨車",50,0, 5);
che xiaokeche=new keche(3,"小客車",40,4,0);
che dakeche=new keche(4,"大客車",80,10,0);
che xiaopika=new pika(5,"小皮車",60,2,4);
che dapika=new pika(6,"大皮車",100,4,8);
che[] c=new che[6];
c[0]=xiaohuoche;c[1]=dahuoche;c[2]=xiaokeche;
c[3]=dakeche;c[4]=xiaopika;c[5]=dapika;
yonghu yh=new yonghu();
che[] c_zuche = null;
while(true){
System.out.println("---------神舟租車系統(tǒng)-----------");
System.out.println("請(qǐng)輸入你的操作:1.登錄? 2.注冊? 3.跳出系統(tǒng)");
Scanner s=new Scanner(System.in);
int num=s.nextInt();
if(num==1){
System.out.println("----執(zhí)行登錄操作----");
System.out.println("請(qǐng)輸入用戶名:");
String uname=s.next();
System.out.println("請(qǐng)輸入你的密碼");
String pwd=s.next();
if(uname.equals(yh.uname)&&pwd.equals(yh.pwd)){
System.out.println("登錄成功!");
break;
}else{
System.out.println("登錄失敗");
}
}else if(num==2){
System.out.println("----執(zhí)行注冊操作----");
System.out.println("請(qǐng)輸入用戶名:");
String uname=s.next();
System.out.println("請(qǐng)輸入你的密碼");
String pwd=s.next();
System.out.println("請(qǐng)輸入你的姓名");
String name=s.next();
yh.uname=uname;
yh.pwd=pwd;
yh.name=name;
System.out.println("注冊成功!");
}else if(num==3){
System.exit(0);
}
}
//租車操作
while(true){
System.out.println("歡迎,"+yh.name+"請(qǐng)選擇你的操作:");
System.out.println("1.查看車輛列表? 2.進(jìn)行租車? 3.查看我的租車總價(jià)格,總載人量,總載物量 4.跳出系統(tǒng)");
Scanner s1=new Scanner(System.in);
int num=s1.nextInt();
if(num==1){
for(che i:c){/*相當(dāng)于for(int i=0;i<c.length;i++)*/
i.jieshao();
}
}else if(num==2){
System.out.println("請(qǐng)輸入你要租的車輛數(shù)");
int num1=s1.nextInt();
c_zuche=new che[num1];
for(int i=0;i<num1;i++){
System.out.println("請(qǐng)輸入第"+(i+1)+"車輛的編號(hào)");
int bianhao=s1.nextInt();
switch(bianhao){
case 1:
c_zuche[i]=xiaohuoche;break;
case 2:
c_zuche[i]=dahuoche;break;
case 3:
c_zuche[i]=xiaokeche;break;
case 4:
c_zuche[i]=dakeche;break;
case 5:
c_zuche[i]=xiaopika;break;
case 6:
c_zuche[i]=dapika;break;
}
}
System.out.println("租車成功!");
}else if(num==3){
if(c_zuche==null){
System.out.println("你還沒有進(jìn)行租車呢!");
}else{
int zongjiage=0;
int zongzairenliang=0;
int zongzaiwuliang=0;
for(che i:c_zuche){
i.jieshao();
zongjiage+=i.jiage;
zongzairenliang+=i.zaikeliang;
zongzaiwuliang+=i.zaiwuliang;
}
System.out.println("總價(jià)格"+zongjiage+"總載人量"+
zongzairenliang+"總載物量"+zongzaiwuliang);
}
}else{
System.exit(0);/*終止系統(tǒng),不可為break,break是跳出循環(huán)*/
}
}
}
}
class yonghu{
String uname;
String pwd;
String name;
}
abstract class che{
int bianhao;
String name;
int jiage;
int zaikeliang;
int zaiwuliang;
public che(int bianhao, String name, int jiage, int zaikeliang,
int zaiwuliang) {
this.bianhao = bianhao;
this.name = name;
this.jiage = jiage;
this.zaikeliang = zaikeliang;
this.zaiwuliang=zaiwuliang;
}
abstract void jieshao();
}
class huoche extends che {
public huoche(int bianhao, String name, int jiage,
int zaikeliang,int zaiwuliang) {
super(bianhao,name,jiage, zaikeliang,zaiwuliang);
}
void jieshao() {
System.out.println("編號(hào)"+this.bianhao+"\t名稱"+this.name+
"\t價(jià)格"+this.jiage+"\t載物量"+this.zaiwuliang);
}
}
class keche extends che{
public keche(int bianhao, String name, int jiage, int zaikeliang,
int zaiwuliang) {
super(bianhao, name, jiage, zaikeliang, zaiwuliang);
}
void jieshao() {
System.out.println("編號(hào)"+this.bianhao+"\t名稱"+
this.name+"\t價(jià)格"+this.jiage+"\t載人量"+this.zaikeliang);
}
}
class pika extends che{
public pika(int bianhao, String name, int jiage, int zaikeliang,
int zaiwuliang) {
super(bianhao, name, jiage, zaikeliang, zaiwuliang);
}
void jieshao() {
System.out.println("編號(hào)"+this.bianhao+"\t名稱"+
this.name+"\t價(jià)格"+this.jiage+"\t載人量"+this.zaikeliang+"載物量"+
this.zaiwuliang);
}
}


