• 充分利用现有对象


    继承,超类遗传给子类。

    继承的威力

      Java类被组织成金字塔型的类层次结构,其中所有的类都是从Object类派生而来的。

      类继承其上面的所有的超类。要了解其中的原理,来看看JApplet类。这个类是所有applet的超类,后者是使用图形用户界面框架(称之为Swing)而且基于浏览器Java程序。JApplet是Applet的子类。

      

      最是顶端是Object类。在继承层次结构中,JApplet有5个超类:Applet、Panel、Container、Component、Object。

      JApplet从这些类继承属性和行为,因为在超类继承层次结构中,它们都在JApplet的正上方。

      1)继承行为和属性

      类的行为和属性由两部分组成:自己的行为和属性以有从超类继承的行为和属性。

      2)覆盖方法

      当方法在子类和超类中都定义了时,将使用子类中的定义:因此子类可以修改、替换或完全删除超类的行为和属性。

      在子类中创建新方法以修改从超类继承来的行为被称为"覆盖"(overriding)方法。如果继承的行为不能产生所需的结果,则需要覆盖相应的方法。

    建立继承

      使用关键字extends将一个类指定为另一个类的子类。

      关键字this用于引用当前对象,关键字supper的用途与此类似:引用对象的上一级超类。

      为了关联构造函数,在子类的构造函数中,第一条语句必须调用超类的构造函数。因此需要使用关键字super。

      如果不使用supper()来调用超类的构造函数,则在子类构告这函数执行时,Java将自动调用无参数的超类构造函数。如果该超类构造函数不存在或提供了意料之外的行为,将导致错误,因此最好手工调用超类的构造函数。

    使用现有的对象

      面向对象编程鼓励代码重用。如查Java类是设计良好的,完全可以在其他程序中使用它。

    将相同类的对象存储到数组列表中

      存储数据最用的类ArrayList,这是一个存储同类对象的数据结构,其大小可以随时调整。

      ArrayList类位于java.unil类包中,这是Java类库中最有用的一个包。在程序中使用它,可使用一条import语句:import java.util.ArrayList;

      数组列表存储的对象要么属于同一个类,要么有相同的超类。要创建数组列表,需要引用两个类:ArrayList类和列表要存储的类。

      将要在列表中存储的类的名称放在"<"和">"之间,如下述语句所示:

    ArrayList<String> structure = new ArrayList<String>(); 

      泛型用来指示数据结构(比如数组列表)可以存储的对象类型。泛型使代码更可靠,因为它向编译器提供了一种防止产生更多错误的方法。

      与数组不同,数组列表存储的元素数量并非固定的。列表在创建时包含10个元素,如查你知道需要存储更多的对象,可通过构造函数参数指定长度。下面的语句创建一个包含300个元素的列表

    ArrayList<String> structure = new ArrayList<String>(300);

      要将对象加入列表中,可调用列表的add()方法,并将对象作为其唯一的参数;

    structure.add("Vance");

      要从列表中检索元素,可使用方法get()并将元素的索引号作为参数。

    String name = structure.get(0);

      要查看列表的元素中是否包含某个对象,可调用contains()方法并将该对象作为参数:

      if (structure.contains("Velma")) {
    
        System.out.println("Velma found");
    
      }

      要将对象从列表中删除, 可调用remove()方法并将该对象或其索引号作为参数:

        structure.remove(0);
        structure.remove("Verson");

       遍历数组列表

        Java包含了一个特殊的for循环以方便载入一个数组列表以及依次检查其每个元素。

    1         ArrayList<String> structure = new ArrayList<String> (3);
    2         structure.add("Lei");
    3         structure.add("Li");
    4         structure.add("Zhang");
    5         for (String name :structure) {
    6             System.out.println(name);
    7         }
     1 import java.util.*;
     2  
     3 public class StringLister {
     4     String[] names = { "Spanky", "Alfalfa", "Buckwheat", "Daria",
     5         "Stymie", "Marianne", "Scotty", "Tommy", "Chubby" };
     6      
     7     public StringLister(String[] moreNames) {
     8         ArrayList<String> list = new ArrayList<String>();
     9         for (int i = 0; i < names.length; i++) {
    10             list.add(names[i]);
    11         }
    12         for (int i = 0; i < moreNames.length; i++) {
    13             list.add(moreNames[i]);
    14         }
    15         Collections.sort(list);
    16         for (String name : list) {
    17             System.out.println(name);
    18         }
    19     }
    20  
    21     public static void main(String[] arguments) {
    22         StringLister lister = new StringLister(arguments);
    23     }
    24 }
    View Code

      使用Collection类的一个方法对存储在列表中的字符串按字母顺序排列,Collections.sort(list);

    创建子类

      下面的例子讲述子类继承超类,并拥有自己的特性

     1 import java.awt.*;
     2 
     3 public class Point3D extends Point {
     4     public int z;
     5 
     6     public Point3D(int x, int y, int z) {
     7         super(x,y);
     8         this.z = z;
     9     }
    10 
    11     public void move(int x, int y, int z) {
    12         this.z = z;
    13         super.move(x, y);
    14     }
    15 
    16     public void translate(int x, int y, int z) {
    17         this.z += z;
    18         super.translate(x, y);
    19     }
    20 }
     1 import java.awt.*;
     2 
     3 class PointTester {
     4     public static void main(String[] arguments) {
     5         Point location1 = new Point(11,22);
     6         Point3D location2 = new Point3D(7,6,64);
     7  
     8         System.out.println("The 2D point is at (" + location1.x
     9             + ", " + location1.y + ")");
    10         System.out.println("It's being moved to (4, 13)");
    11         location1.move(4,13);
    12         System.out.println("The 2D point is now at (" + location1.x
    13             + ", " + location1.y + ")");
    14         System.out.println("It's being moved -10 units on both the x "
    15             + "and y axes");
    16         location1.translate(-10,-10);
    17         System.out.println("The 2D point ends up at (" + location1.x
    18             + ", " + location1.y + ")
    ");
    19  
    20         System.out.println("The 3D point is at (" + location2.x
    21             + ", " + location2.y + ", " + location2.z + ")");
    22         System.out.println("It's being moved to (10, 22, 71)");
    23         location2.move(10,22,71);
    24         System.out.println("The 3D point is now at (" + location2.x
    25             + ", " + location2.y + ", " + location2.z + ")");
    26         System.out.println("It's being moved -20 units on the x, y "
    27             + "and z axes");
    28         location2.translate(-20,-20,-20);
    29         System.out.println("The 3D point ends up at (" + location2.x
    30             + ", " + location2.y + ", " + location2.z + ")");
    31     }
    32 }
    PointTest

    问与答:

       1.Java程序没有使用extends来从超类继承,这是否意味着它们不在层次结构中?

        答:使用Java创建的所有类都是Java类层次结构的一部分,因为编定程序时如果不使用关键字extends,则默认超类为Object对象。所有类的方法equals()和toString()都是自动从Object类继承而来的行为。 

  • 相关阅读:
    centos7下mysql双主+keepalived
    Nginx 性能优化有这篇就够了!
    mysql对自增主键ID进行重新排序
    nginx 配置文件 2019-12-20
    zabbix服务端接收的数据类型,便于编写脚本向服务端提交数据
    zabbix自动注册,实现自动添加机器,减少人工干预
    zabbix企业微信告警配置教程
    websocket 连接测试端口服务是否正常代码
    mongodb Sort排序能够支持的最大内存限制为32M Plan executor error during find: FAILURE
    rabbitmq 的安装配置使用
  • 原文地址:https://www.cnblogs.com/dulixiaoqiao/p/6417919.html
Copyright © 2020-2023  润新知