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)

  • 相关阅读:
    rabbitmq队列
    什么是RPC
    图解rabbitmq
    rabbitmq的面试题
    nginx介绍
    nginx正向代理和反向代理
    负载均衡 分布式 集群
    Nginx的负载均衡
    缓存雪崩 穿透 击穿
    Python Day32:UDP协议、UDP与TCP协议的区别、以及其服务端、客户端标准代码
  • 原文地址:https://www.cnblogs.com/redlogic/p/8626218.html
Copyright © 2011-2022 走看看