Java零基础程序阅读三

初南电脑学习 2024-02-21 08:31:32
找出下列程序中的问题:public Person {// 创建人类String flag;// 用来表示年龄段,例如“老人”,“小孩”等int age = 70;// 用来表示年龄,并初始化为70public Person(String flag, int age) {// 人类的有参构造方法this.flag = flag;// 把参数的值赋给属性}public String toString() {// 重写toString()方法,输出对象时自动调用return this.flag + ":" + this.age + "岁。";// 返回的字符串}public static void main(String[] args) {Person person1 = new Person("老人", 70);// 创建人类对象person1,参数的值分别为“老人”和“70”System.out.println(person1);// 输出人类对象person1Person person2 = new Person("小孩", 10);// 创建人类对象person2,参数的值分别为“小孩”和“10”System.out.println(person2);// 输出人类对象person2}}2.找出下列程序中的问题: abstract BuyOranges {// 创建买橘子类public static void main(String[] args) {BuyOranges bo = new BuyOranges();// 创建买橘子类的对象double unitPrice = 2.98;// 声明橘子的单价double weight = 6.0;// 声明橘子的重量// 输出买橘子需要支付的金额System.out.println("橘子2.98元/斤,购买6斤需支付" + bo.pay(unitPrice, weight) + "元RMB。");}private double pay(double unitPrice, double weight) {// 支付方法,参数分别为“橘子的单价”和“橘子的重量”double money = unitPrice * weight;// 计算买橘子需要支付的金额return money;// 返回买橘子需要支付的金额}}3、找出下列程序中的问题: class GasStation {// 加油站类public int addOil(int oilVolume) {// 加汽油方法oilVolume += 2;// 每次加2个单位的汽油return oilVolume;// 返回添加的汽油油量}}public AutoMobile {// 机动车类public static void main(String[] args) {int leftOilVolume = 10;// 机动车剩余的汽油油量GasStation gs = null;// 声明加油站类的对象,并初始化为nullfor (int i = 1; i
0 阅读:10

初南电脑学习

简介:感谢大家的关注