• 《Java技术》第三次作业


    (一)学习总结

    1.阅读下面程序,分析是否能编译通过?如果不能,说明原因。应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来?

    class Grandparent {
        public Grandparent() {
            System.out.println("GrandParent Created.");
        }
        public Grandparent(String string) {
            System.out.println("GrandParent Created.String:" + string);
        }
    }
    class Parent extends Grandparent {
        public Parent() {        
            System.out.println("Parent Created");
            super("Hello.Grandparent.");
        }
    }
    class Child extends Parent {
        public Child() {
            System.out.println("Child Created");
        }
    }
    public class Test{
        public static void main(String args[]) {
            Child c = new Child();
        }
    }
    

    1:编译不能通过。
    2:原因及修改:使用super关键字时,该语句必须放在子类构造方法的首位
    3:运行结果:
    GrandParent Created.String:Hello.Grandparent.
    Parent Created
    Child Created
    4:子类拥有父类的全部成员变量和方法,不调用父类的构造方法则不能正确的实例化
    5:反过来不行,因为父类不能读取子类的成员变量及方法

    2.阅读下面程序,分析程序中存在哪些错误,说明原因,应如何改正?正确程序的运行结果是什么?

    class Animal{
      void shout(){
          System.out.println("动物叫!");
      }
    }
    class Dog extends Animal{
          public void shout(){  
              System.out.println("汪汪......!");  
         }
          public void sleep() {
           System.out.println("狗狗睡觉......");
          } 
    }
    public class Test{
        public static void main(String args[]) {
            Animal animal = new Dog(); 
            animal.shout();
            animal.sleep();
            Dog dog = animal;
            dog.sleep(); 
            Animal animal2 = new Animal();
            dog = (Dog)animal2;
            dog.shout();
        }
    }
    

    1:父类中没有sleep方法

    3.运行下列程序

    class Person { 
       private String name ; 
       private int age ; 
       public Person(String name,int age){ 
             this.name = name ; 
             this.age = age ; 
       } 
    }
    public class Test{  
          public static void main(String args[]){ 
                 Person per = new Person("张三",20) ; 
                 System.out.println(per);
                 System.out.println(per.toString()) ; 
      } 
    }
    

    (1)程序的运行结果如下,说明什么问题?
    程序运行输出的是随机输出了一些地址信息,不是该地址所存储的内容
    (2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
    (3)在Person类中增加如下方法程序的执行结果是什么?说明什么问题?

    public String toString(){ 
            return "姓名:" + this.name + ",年龄:" + this.age ; 
     } 
    

    运行结果:
    姓名:张三,年龄:20
    姓名:张三,年龄:20
    所说明的问题:
    对象输出时一定会调用objekt类中的toSring()方法打印内容

    4.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路。现在要创建一个可租车列表,应当如何创建?
    定义一个车辆 接口包含编号、名称、租金,属性并对每个属性分别定义get,set方法。
    分别定义一个载客量和载货量接口并具有载客量和载货量属性并且定义get,set方法。
    客车类、货车类、皮卡类同时继承车辆接口,客车类继承载客量接口,货车类继承载货量接口,皮卡类同时继承载客量和载货量接口。
    在测试类中完成可租车列表的创建。

    5.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果

    interface Animal{    
            void breathe();
            void run();
            void eat();
        }
        class Dog implements Animal{
            public void breathe(){
                System.out.println("I'm breathing");
            }
            void eat(){
                System.out.println("I'm eating");
            }
        }
        public class Test{
            public static void main(String[] args){
                Dog dog = new Dog();
                dog.breathe();
                dog.eat();
            }
        }
    

    不能通过编译。

    在类的声明中用implements子句来表示一个类使用某个接口,在类中可以使用接口中定义的常量,而且必须实现接口中定义的所有方法。
    在类中实现接口所定义的方法时,必须显式地使用public修饰符,否则将被系统警告为缩小了接口中定义的方法的访问控制范围。

    其他总结:
    super 关键字:
    使用super关键字可以从子类中调用父类中的构造方,普通方法和属性

    super与this的区别:
    1:this是访问本类中的属性,本类中没有找到此属性,则从父类中继续查找,super是访问父类中的属性
    2:this调用本类构造,必须放在构造方法的属性,super调用父类构造,必须放在子类构造方法的首行

    final关键字:
    1:final在Java中表示最终的意思,也可以成为完结器。可以使用final关键字生命类,属性,方法,在声明时需要注意:
    (1):使用final声明的类不能有子类
    (2):使用final声明的方法不能被子类覆盖
    (3):使用final声明的变量即成为常量,常量不能修改

    (二)实验总结
    1.银行新用户现金业务办理
    设计思路:

    (1)定义银行类Bank:银行名称bankName(静态变量)、用户名name、密码password、账号余额balance、交易额turnover。
    包括如下方法:
    静态方法welcome():打印欢迎语
    构造方法:实现新用户的开户。包括用户名,密码,交易额。开户时扣除10元开卡费。
    存款方法deposit():根据存款额修改账户余额。输出相关信息。
    取款方法withdrawal():对用户密码进行验证,密码错误或取款额大于余额,不能办理业务,并提示用户。否则,修改用户余额。
    静态方法welcomeNext():输出欢迎下次光临。

    码云地址:
    实验四:
    https://gitee.com/myf91/Java_CS01myf/tree/master/ex04
    实验五:
    https://gitee.com/myf91/Java_CS01myf/tree/master/ex05/声音模拟器

  • 相关阅读:
    May 9, 17:3020:00, 1479, "Concentrationcompactness/Rigidity method" lecture 4 by Yang Lan
    May 21, 10:0011:00, 1303, "Capacity, Primeter and ADMmass " by Jie Xiao
    课程期末安排
    问题: Schrodinger方程的广义Strichartz估计
    May 21, 11:1012:10 "On small solutions for nonlinear wave equations in exterior domains " by Makoto NAKAMURA
    SQL Server2019数据库备份与还原脚本,数据库可批量备份
    记一次 IIS 站点配置文件备份和还原,物理路径文件批量备份
    从网上收集的一些图片方面的操作
    android获取本地IP
    java利用httpconnect向服务器发送信息与接收回馈信息
  • 原文地址:https://www.cnblogs.com/myfdpk/p/8858522.html
Copyright © 2020-2023  润新知