zoukankan      html  css  js  c++  java
  • 从零自学Java-10.充分利用现有对象

    1.超类和子类的设计;
    2.建立继承层次;
    3.覆盖方法。

    程序StringLister:使用数组列表和特殊的for循环将一系列字符串按字母顺序显示到屏幕上。这些字符串来自一个数组和命令行参数

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

    输出:

    Buckwheat
    Chubby
    Daria
    Marianne
    Scotty
    Spanky
    Stymie
    Tommy

    程序PointTester:使用Point,Point3D,Point4D对象的程序,并在屏幕上移动它们。

     1 package com.jsample;
     2 
     3 import java.awt.*;
     4 
     5 public class PointTester {
     6     public static void main(String[] args){
     7         Point objcet1 = new Point(11,22);
     8         Point3D object2 = new Point3D(7,6,64);
     9         Point4D object3 = new Point4D(12,56,73,90);
    10 
    11         System.out.println("The 2D point is located at (" + objcet1.x
    12                 + ", " + objcet1.y + ")");
    13         System.out.println("	It's being moved to (4,13)");
    14         objcet1.move(4,13);
    15         System.out.println("The 2D point is now at (" + objcet1.x
    16             + ", " + objcet1.y + ")");
    17         System.out.println("	It's being moved -10 units on the x "
    18             + "and y axes");
    19         objcet1.translate(-10,-10);
    20         System.out.println("The 2D point ends up at (" + objcet1.x
    21             + ", " + objcet1.y + ")
    ");
    22 
    23         System.out.println("The 3D point is located at (" + object2.x
    24             + ", " + object2.y + ", " + object2.z + ")");
    25         System.out.println("	It's being moved to (10, 22, 71)");
    26         object2.move(10,22,71);
    27         System.out.println("The 3D point is now at (" + object2.x
    28             + ", " + object2.y + ", " + object2.z +")");
    29         System.out.println("	It's being moved -20 units on the x,y "
    30             + "and z axes");
    31         object2.move(-20,-20,-20);
    32         System.out.println("The 3D point ends up at (" + object2.x
    33             + ", " + object2.y + ", " + object2.z + ")
    ");
    34 
    35         System.out.println("The 4D point is located at (" + object3.x
    36                 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");
    37         System.out.println("	It's being moved to (9, 1, 7, 4)");
    38         object3.move(9,1,7,4);
    39         System.out.println("The 4D point is now at (" + object3.x
    40                 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");
    41         System.out.println("	It's being moved 20 units on the x,y "
    42                 + "and z axes");
    43         object3.move(20,20,20,20);
    44         System.out.println("The 4D point ends up at (" + object3.x
    45                 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");
    46     }
    47 }
    View Code

    下属class:

    Point——java.awt.*中自带。

    Point3D:记录对象三维坐标,将对象移到新坐标处,将三个坐标各移动特定距离。

     1 package com.jsample;
     2 
     3 import java.awt.*;
     4 
     5 public class Point3D extends Point{
     6     public int z;
     7 
     8     public Point3D(int x, int y, int z){
     9         super(x,y);
    10         this.z=z;
    11     }
    12 
    13     public void move(int x, int y, int z){
    14         this.z += z;
    15         super.translate(x,y);
    16     }
    17 }
    View Code

    Point4D:记录对象四维坐标,将对象移到新坐标处,将四个坐标各移动特定距离。

     1 package com.jsample;
     2 
     3 public class Point4D extends Point3D{
     4     public int t;
     5 
     6     public Point4D(int x, int y, int z, int t){
     7         super(x,y,z);
     8         if (t < 0)
     9             return;
    10         this.t = t;
    11     }
    12 
    13     public void move(int x, int y, int z, int t){
    14         this.z += z;
    15         this.t += t;
    16         super.translate(x,y);
    17     }
    18 }
    View Code

    输出:

    The 2D point is located at (11, 22)
     It's being moved to (4,13)
    The 2D point is now at (4, 13)
     It's being moved -10 units on the x and y axes
    The 2D point ends up at (-6, 3)

    The 3D point is located at (7, 6, 64)
     It's being moved to (10, 22, 71)
    The 3D point is now at (17, 28, 135)
     It's being moved -20 units on the x,y and z axes
    The 3D point ends up at (-3, 8, 115)

    The 4D point is located at (12, 56, 73, 90)
     It's being moved to (9, 1, 7, 4)
    The 4D point is now at (21, 57, 80, 94)
     It's being moved 20 units on the x,y and z axes
    The 4D point ends up at (41, 77, 100, 114)

  • 相关阅读:
    12套有用的免费 PSD 格式 Android UI 素材
    使用 Canvas 和 JavaScript 创建逼真的下雨效果
    在网页设计中使用漂亮字体的16个优秀例子
    Koala – 开源的前端预处理器语言图形编译工具
    BackgroundCheck – 根据图片亮度智能切换元素样式
    经典网页设计:18个示例展示图片在网页中的使用
    TogetherJS – 酷!在网站中添加在线实时协作功能
    30个令人惊叹的灵感来自自然风光的网站设计《下篇》
    太有才了!创新的街头涂鸦手绘欣赏【中篇】
    15款美丽的设备模板,帮助展示你的 APP
  • 原文地址:https://www.cnblogs.com/redlogic/p/8626218.html
Copyright © 2011-2022 走看看