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

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

 package com.jsample;

 import java.util.*;

 public class StringLister {
String[] names = { "Spanky", "Buckwheat", "Daria",
"Stymie", "Marianne", "Scotty", "Tommy", "Chubby" }; public StringLister(String[] moreNames){
Vector<String> list = new Vector<String>();
for (int i = 0; i < moreNames.length; i++){
list.add(moreNames[i]);
}
Collections.sort(list);
for (String name : list){
System.out.println(name);
}
} public static void main(String[] args){
StringLister lister = new StringLister(args);
}
}

输出:

Buckwheat
Chubby
Daria
Marianne
Scotty
Spanky
Stymie
Tommy

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

 package com.jsample;

 import java.awt.*;

 public class PointTester {
public static void main(String[] args){
Point objcet1 = new Point(11,22);
Point3D object2 = new Point3D(7,6,64);
Point4D object3 = new Point4D(12,56,73,90); System.out.println("The 2D point is located at (" + objcet1.x
+ ", " + objcet1.y + ")");
System.out.println("\tIt's being moved to (4,13)");
objcet1.move(4,13);
System.out.println("The 2D point is now at (" + objcet1.x
+ ", " + objcet1.y + ")");
System.out.println("\tIt's being moved -10 units on the x "
+ "and y axes");
objcet1.translate(-10,-10);
System.out.println("The 2D point ends up at (" + objcet1.x
+ ", " + objcet1.y + ")\n"); System.out.println("The 3D point is located at (" + object2.x
+ ", " + object2.y + ", " + object2.z + ")");
System.out.println("\tIt's being moved to (10, 22, 71)");
object2.move(10,22,71);
System.out.println("The 3D point is now at (" + object2.x
+ ", " + object2.y + ", " + object2.z +")");
System.out.println("\tIt's being moved -20 units on the x,y "
+ "and z axes");
object2.move(-20,-20,-20);
System.out.println("The 3D point ends up at (" + object2.x
+ ", " + object2.y + ", " + object2.z + ")\n"); System.out.println("The 4D point is located at (" + object3.x
+ ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");
System.out.println("\tIt's being moved to (9, 1, 7, 4)");
object3.move(9,1,7,4);
System.out.println("The 4D point is now at (" + object3.x
+ ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");
System.out.println("\tIt's being moved 20 units on the x,y "
+ "and z axes");
object3.move(20,20,20,20);
System.out.println("The 4D point ends up at (" + object3.x
+ ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");
}
}

下属class:

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

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

 package com.jsample;

 import java.awt.*;

 public class Point3D extends Point{
public int z; public Point3D(int x, int y, int z){
super(x,y);
this.z=z;
} public void move(int x, int y, int z){
this.z += z;
super.translate(x,y);
}
}

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

 package com.jsample;

 public class Point4D extends Point3D{
public int t; public Point4D(int x, int y, int z, int t){
super(x,y,z);
if (t < 0)
return;
this.t = t;
} public void move(int x, int y, int z, int t){
this.z += z;
this.t += t;
super.translate(x,y);
}
}

输出:

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)

从零自学Java-10.充分利用现有对象的更多相关文章

  1. 从零自学Hadoop(10):Hadoop1.x与Hadoop2.x

    阅读目录 序 里程碑 Hadoop1.x与Hadoop2.x 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的 ...

  2. 从零自学Hadoop系列索引

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 从零自学Hadoop(01):认识Hadoop ...

  3. 作为一个零基础的新手,如何系统的自学Java和JavaEE开发技术?

    其实这个问题很简单,我用最简单的语言给大家描述一下,学习一样东西就要了解这样东西学完了要干什么事情,有什么作用.然后就是应该学习哪些必要的内容,该如何运用得当的方法进行有效率的学习不至于自己摸不着头脑 ...

  4. JAVA自学笔记10

    JAVA自学笔记10 1.形式参数与返回值 1)类名作为形式参数(基本类型.引用类型) 作形参必须是类的对象 2)抽象类名作形参 需要该抽象类的子类对象,通过多态实现 3)接口名为形参 需要的是该接口 ...

  5. 零基础如何自学java开发?

    开篇直奔主题,java 学习个人感觉分为两种途径,第一种是在学校,在培训机构等地方学习. 有人指导:第二种是自学,通过视频,书籍,朋友等完成学习. 本文适合 自学,且基础薄弱或者无基础的人.先介绍下我 ...

  6. 全套Java教程_Java基础入门教程,零基础小白自学Java必备教程 #011 # 第十一单元 String&ArrayList #

    一.本单元知识点概述 (Ⅰ)知识点概述 二.本单元教学目标 (Ⅰ)重点知识目标 1.ArrayList集合的常用方法2.ArrayList存储数据和遍历数据3.String类的构造方法4.String ...

  7. Java中10个流对象重点掌握

    目前为止,10个流对象重点掌握: 字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileO ...

  8. 【Java并发.4】对象的组合

    到目前为止,我们已经介绍了关于线程安全与同步的一些基础知识.然而,我们并不希望对每一系内存访问都进行分析以确保程序是线程安全的,而是希望将一些现有的线程安全组件组合为更大规模的组件或程序. 4.1 设 ...

  9. 拜托,别再问我怎么自学 Java 了!和盘托出

    假如有那么残酷的一天,我不小心喝错了一瓶药,一下子抹掉了我这十多年的编程经验,把我变成了一只小白.我想自学 Java,并且想要找到一份工作,我预计需要 6 个月的时间,前提条件是每天都处于高效率的学习 ...

随机推荐

  1. Windows10开发手记-RelativePanel使用详解

    Windows 10已于7月29号面向全球发布,同时Universal Windows Platform(UWP) SDK也已正式放出,配合VS 2015我们可以开发出通用的Windows App. ...

  2. 内存管理buddy[原理]

    TODO------------------------------------------------------------------------------------------------ ...

  3. Spring 源码分析之 bean 实例化原理

    本次主要想写spring bean的实例化相关的内容.创建spring bean 实例是spring bean 生命周期的第一阶段.bean 的生命周期主要有如下几个步骤: 创建bean的实例 给实例 ...

  4. 非table结构数据导入excel

    现在大部分的数据都是ul li 展示,一下提供方法 <!DOCTYPE html><html> <head> <meta charset="UTF- ...

  5. 对nginx中location的认识

    关于一些对location认识的误区 1.location的匹配顺序是“先匹配正则,在匹配普通”. location的匹配顺序其实是“先匹配普通,在匹配正则”.造成误解的原因是:正则匹配会覆盖普通匹配 ...

  6. .net core + headless chrome实现动态网页爬虫

    一般的http请求库只能够抓取到网页的静态内容,如果想抓取通过js动态生成的内容可以使用没有gui的browser库,之前许多人会使用phantomjs作为headless browser,不过现在p ...

  7. div盒子水平垂直居中方法

    文章转载自:div盒子水平垂直居中的方法 - 雪明瑶 这个问题比较老,方法比较多,各有优劣,着情使用. 一.盒子没有固定的宽和高 方案1.Transforms 变形 这是最简单的方法,不仅能实现绝对居 ...

  8. ETCD&Flannel安装

    .ETCD 安装: nohup etcd --name etcd0 \ --advertise-client-urls http://172.31.24.246:2379,http://127.0.0 ...

  9. MRTG在Windows平台的安装及使用

    MRTG (Multi Router Traffic Grapher)是一款监控网络流量负载的免费软件,目前利用MRTG已经开发出了各式各样的统计系统: 1.系统资源负载统计,例如:磁盘空间.CPU负 ...

  10. array与xml转换实现(转)

    <?php function xml_encode($data, $charset = 'utf-8', $root = 'so') { $xml = '<?xml version=&qu ...