zoukankan      html  css  js  c++  java
  • java3

    (一)学习总结

    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();
        }
    }
    

    不能通过编译,父类中已经有含参方法,继承了爷爷类,所以应该用super继承

    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() {        
            super("Hello.Grandparent.");
            System.out.println("Parent Created");
            
        }
    }
    class Child extends Parent {
        public Child() {
            System.out.println("Child Created");
        }
    }
    public class Test{
        public static void main(String args[]) {
            Child c = new Child();
        }
    }
    

    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();
        }
    }
    

    没有为类型 Animal 定义方法 sleep();
    类型不匹配:不能从 Animal 转换为 Dog
    程序有错误,,,

    然后改写了

    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 = (Dog) animal;
            dog.sleep(); 
            Animal animal2 = new Animal();
            if(animal2 instanceof Dog)
            {
                dog = (Dog)animal2;
                dog.shout();
            }
        }
    }
    
    1. animal是子类Dog的向上转型对象,向上转型对象不能操作子类新增加的成员变量
      父类对象定义为子类对象称为向下转型,对于向下转型,需要强制转型,
      即必须明确指明要转型的子类类型: 格式:子类名称 子类对象 =(子类)父类实例;

    2. animal2是一个新定义的父类对象,即父类引用的对象是父类本身。父类对象不能确定dog是自己的子类。需要使用instanceof关键字避免此错误。

    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)程序的运行结果如下,说明什么问题?

    Person@166afb3
    Person@166afb3

    System.out.println(per);默认调用父类Object 的toString方法。及运行两次相同程序,所以他运行程序一样

    (2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
    然后不知道源码知道在上边有源码,不过不太会用
    源码就是指编写的最原始程序的代码。运行的软件是要经过编写的,程序员编写程序的过程中需要他们的“语言”。音乐家用 五线谱,建筑师用图纸,那程序员的工作的语言就是“源码”了。
    人们平时使用软件时就是程序把“源码”翻译成我们可直观的形式表现出来供我们使用的。 [1]
    任何一个网站页面,换成源码就是一堆按一定格式书写的文字和符号,但我们的浏览器帮我们翻译成眼前的模样了。

    public void println(Object x) {
         String s = String.valueOf(x);
         synchronized (this) {
         print(s);
         newLine();
         }
     }
    
     valueOf(x) //if the argument is null, then a string equal to "null";
     otherwise, the value of obj.toString() is returned.
    

    (3)在Person类中增加如下方法

    public String toString(){ 
            return "姓名:" + this.name + ",年龄:" + this.age ; 
     } 
     
    重新运行程序,程序的执行结果是什么?说明什么问题?
    package text;
    class Person { 
       private String name ; 
       private int age ; 
       public Person(String name,int age){ 
             this.name = name ; 
             this.age = age ; 
       } 
    
    public String toString(){ 
            return "姓名:" + this.name + ",年龄:" + this.age ; 
     } 
    }
    public class text{  
          public static void main(String args[]){ 
                 Person per = new Person("张三",20) ; 
                 System.out.println(per);
                 System.out.println(per.toString()) ; 
      } 
    }
    

    姓名:张三,年龄:20
    姓名:张三,年龄:20
    说明在Person类中完成了对父类Object的toString类的重写。

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

    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();
            }
        }
    

    :接口中方法,默认是public abstract 所以在子类实现抽象方法时,应该用public修饰

        interface Animal{    
            void breathe();
            void run();
            void eat();
        }
        class Dog implements Animal{
            public void breathe(){
                System.out.println("I'm breathing");
            }
            public void run()
            {
                
            }
            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();
            }
        }
    

    源码就是指编写的最原始程序的代码。运行的软件是要经过编写的,程序员编写程序的过程中需要他们的“语言”。音乐家用 五线谱,建筑师用图纸,那程序员的工作的语言就是“源码”了。
    人们平时使用软件时就是程序把“源码”翻译成我们可直观的形式表现出来供我们使用的。 [1]
    任何一个网站页面,换成源码就是一堆按一定格式书写的文字和符号,但我们的浏览器帮我们翻译成眼前的模样了。

    多态性是指同名的不同方法在程序中共享,即为同一个方法定义几个版本,运行时根据不同情况执行不同的版本。
    一个名字,多个方法
    多态性的实现:
    覆盖实现多态:(子类重写父类中的方法)
    重载实现多态(同一个类中的同名方法)
    相同的一条语句在不同的运行环境中可以产生不同的运行结果。

    覆盖实现多态:

    子类方法的返回类型、参数、方法名称要和父类的返回类型、参数、方法名称完全一样,否则编译出错。
    被覆盖的方法不能为private
    子类方法不能缩小父类方法的访问权限。
    覆盖后的方法不能比被覆盖的方法产生更多的例外,即抛出更多的异常。
    根据调用对象区分调用的方法
    重载实现多态:

    方法名相同
    方法的参数类型,个数,顺序至少有一项不同
    根据形参来决定调用的是哪个方法

    接口与类的不同在于:

    (1) 没有变量的声明,但可以定义常量。
    (2) 只有方法的声明,没有方法的实现。

    接口只是一个特殊的类,只包含全局常量和抽象方法,
    接口的抽象方法可以不加abstract。
    抽象类的抽象方法必须有abstract关键字。
    一个类只能继承一个父类,但是可以同时实现多个接口。
    一个接口可以同时继承多个接口(extends),以实现接口的多继承。
    接口和抽象类一样,都必须依靠子类。
    一个抽象类可以实现多个接口,但是一个接口不能继承一个抽象类。

    接口的定义:
    [public] interface 接口名称 [extends 父接口列表]
    {
    //抽象方法和全局常量
    }
    public指明任意类均可使用这个接口,缺省时,表明只有与接口定义在同一个包中的类才可访问此
    extends是关键字,父接口列表表示一个接口可以有多个父接口,用逗号分开,而类只能有一个父类。子接口继承父接口中所有的常量和方法。
    接口中没有自身的构造方法。
    在接口中定义的常量必须是pubilc static final,这是系统默认的规定,所以常量也可以没有任何修饰符。
    接口中只有方法声明,而无方法实现,接口中声明的方法必须是public abstract,也是系统默认的规定。
    接口定义的完整格式:
    interface A{
    public static final String AUTHOR = "李兴华" ; // 定义全局常量
    public abstract void print() ; // 定义抽象方法
    public abstract String getInfo() ; // 定义抽象方法
    }
    接口定义的简化格式:

    interface A{
    String AUTHOR = "李兴华" ; // 定义全局常量
    void print() ; // 定义抽象方法
    String getInfo() ; // 定义抽象方法
    }
    接口的实现:
    格式:

    class 类名 implements 接口列表 { 类体 }
    

    (1)在类的声明中用implements子句来表示一个类使用某个接口,在类中可以使用接口中定义的常量,而且必须实现接口中定义的所有方法。
    (2)一个类可实现多个接口,在implements子句中用逗号分隔。
    注意:

    在类中实现接口所定义的方法时,方法的声明必须与接口中所定义的完全一致。
    在类中实现接口所定义的方法时,必须显式地使用public修饰符,否则将被系统警 告为缩小了接口中定义的方法的访问控制范围。
    抽象类可以不实现接口的抽象方法,而非抽象类必须实现接口中的所有方法。
    接口的继承:
    格式:
    interface 子接口 extends 父接口A,父接口B,…{
    }

    然后学长。。。电脑中病毒了,U盘丢了,我手机没法继续编辑,我就写评论里了,1、程序设计思路:定义一个银行类,包含静态方法欢迎语,构造方法,存款方法,取款方法,静态方法结束语。交易类用于模拟测试。
    问题1:银行bank类,那个switch语句我没有用在text类里,一开始理解错意思了,把他放在打印欢迎语句类里了,然后就与题意不符

    解决方案:就是改了位置,然后我打印语句时,他无法持续操作,后来加了循环,问的同学
    2、程序设计思路:继承父类所有方法,并增加新的方法。
    3、程序设计思路:平面图型类分别被圆形类,三角形类,矩形类继承;立体图型类分别被圆柱类,圆锥类,球类继承;在测试类中声明两个父类的对象,分别用于调用相应的子类方法,一个判断周长,面积的方法;一个判断表面积,体积的方法。
    4、程序设计思路:动物类分别被狮子类、猴子类、鸽子类继承,喂食类,测试类分别对对象向上转型实例化,并开辟相应的数组空间,以调用子类重写的方法。
    问题1:向上转型
    原因:没有给数组开辟空间
    解决方案:分别开辟空间
    实验五
    1、程序设计思路:设计宠物接口,猫类和狗类分别实现接口,商店类:一个方法为数组开辟空间,用来存放宠物店里已有宠物;一个添加方法,给数组存储宠物信息;展示宠物方法用来输出店里的所有宠物;一个查找方法,当用户输入要购买的宠物编号时,就在数组中进行查找,找到后就存入一个新的数组,用于最后打印购物清单。一个show类:先向用户展示所有宠物,用户可输入要购买的宠物编号和数量,然后用户可选择继续购买,或是打印清单,最后打印购物清单和价钱合计。这个宠物的我创建了pet父类,dog,cat继承petshop,super语句,然后我的主函数有问题,他改正要我把static删了
    就是让我把 public class Test{
    public static void main(String[] args){
    上的static删了,或者改哪里忘记了,,然后我u盘找到后尽快上传。

  • 相关阅读:
    51nod 1117 聪明的木匠:哈夫曼树
    51nod 1010 只包含因子2 3 5的数
    51nod 2636 卡车加油
    51nod 2989 组合数
    51nod 2652 阶乘0的数量 V2
    51nod 1103 N的倍数
    51nod 2489 小b和灯泡
    51nod 1003 阶乘后面0的数量
    51nod 2122 分解质因数
    javascript中的setter和getter
  • 原文地址:https://www.cnblogs.com/hezhengjia/p/8884452.html
Copyright © 2011-2022 走看看