【作业】第六章 面向对象基础

//计算器简单的加减乘除
import java.util.Scanner;

public class Calculator {
    public static void main(String[] args){
        int jieShu=1;
        do {
            Scanner input = new Scanner(System.in);
            JiSuan jiSuan = new JiSuan();
            System.out.print("请输入你要作的运算(+-*/):");
            jiSuan.fuHao = input.next();
            System.out.print("请输入您要运算的第一个数:");
            jiSuan.num1 = input.nextDouble();
            System.out.print("请输入您要运算的第二个数:");
            jiSuan.num2 = input.nextDouble();
            System.out.println("结果为:" + jiSuan.num1 + jiSuan.fuHao + jiSuan.num2 + "=" + jiSuan.cound());
            System.out.print("输入0继续计算:");
            jieShu = input.nextInt();
        }while(jieShu==0);
        System.out.println("谢谢使用");

    }
}
class JiSuan{
    double sum,num1,num2;
    String fuHao;
    public double cound(){
        switch(fuHao){
            case "+":
                sum = num1 + num2;
                break;
            case "-":
                sum = num1 - num2;
                break;
            case "*":
                sum = num1 * num2;
                break;
            case "/":
                sum = num1 / num2;
                break;
        }
        return sum;
    }
}
---------------------------------------------------------------------------------
两个整数、三个浮点数的加法运算
public class JiaFaYunSuan {
    int num1,num2;
    double num,num3,num4,num5;
    public  JiaFaYunSuan(int num1, int num2){
        num=num1+num2;
        System.out.println(num);
    }
    public JiaFaYunSuan(double num3,double num4,double num5){
        num=num3+num4+num5;
        System.out.println(num);
    }
}
class JieGuo{
    public static void main(String[] args){
        JiaFaYunSuan yunSuan=new JiaFaYunSuan(11.96,12.88,12);
        JiaFaYunSuan yunSuan0=new JiaFaYunSuan(11,12);
    }
}
---------------------------------------------------------------------
如下JAVA代码,编译运行后,输出结果是什么?
public class MobilPhoneTest {
    public static void main(String[] args){     //main方法程序入口
        MobilPhone mp=new MobilPhone();     //创建购买手机类的对象
        mp.brand="苹果";      //“"苹果"赋值给对象mp里的brand
        String detail=mp.buy("发工资啦");   //使用买手机对象带参的有钱方法赋值给detail
        System.out.println(detail);     //输出结果
    }
}
//购买手机类
class MobilPhone{
    public String brand;    //手机品牌
    public MobilPhone(){    //无参构造方法
        this.brand="诺基亚";   //”"诺基亚"赋值刚给成员变量brand
    }
    public MobilPhone(String bra){  //带参构造方法
        this.brand=bra;     //bra赋值给成员变量brand
    }
    public String buy(){    //没发工资,购买手机方法
        return "没发工资,买一个"+brand+"牌子的手机吧!";
    }
    public String buy(String reason){   //带参的有钱方法
        return reason+",快买一个"+brand+"牌子的手机吧!";
    }
}
//结果
//发工资啦,快买一个苹果牌子的手机吧!
---------------------------------------------------------------------------------

 

智能手机和手机,使用接口实现
public interface NetWorking {
    void netWork();
}
---------------------------------
public interface Playing {
    void playVideo(String name);
}
---------------------------------
public interface TakePhoto {
    void takePhotos();
}
-----------------------------------
public abstract class HandSet {
    private String brand;
    private String type;

    public HandSet() {
    }

    public HandSet(String brand, String type) {
        this.brand = brand;
        this.type = type;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    public void showInfo(){
        System.out.println("这是一款序号为"+type+"的"+brand+"手机");
    }
    public abstract void sendMess();
    public abstract void call();
}
public class Test {
    public static void main(String[] args){
        CommonPhone common = new CommonPhone("索尼爱立信","G502c");
        common.showInfo();
        common.call();
        common.sendMess();
        common.playVideo("热雪");
        SmartPhone smartPhone = new SmartPhone("HTC","I9100");
        smartPhone.showInfo();
        smartPhone.call();
        smartPhone.sendMess();
        smartPhone.playVideo("海贼王");
        smartPhone.netWork();
        smartPhone.takePhotos();
    }
}

 

上一篇:SystemVerilog 语言部分(二)


下一篇:四、kubernetes 核心技术-Pod