继承和多态
public class Point2D {
private int x;//定义成私有的在 扩展的类中不能直接调用的
private int y; //定义成私有的在 扩展的类中不能直接调用的 在这里初始值为0
public Point2D() {
}
public Point2D(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public int getX() { return x; }
public int getY() { return y; }
}
public class Point3D extends Point2D { // 扩展Point2D类
private int z; // 新增私有数据
public Point3D() {
super();//调用父类中的没有参数的构造函数
}
// 定义建构方法
Point3D(int x, int y, int z) {
super(x, y); //同时指定呼叫父类构造函数
this.z = z;
}
// 新增方法
public void setZ(int z) { this.z = z; }
public int getZ() { return z; }
}
public class ExtendDemo {
public static void main(String[] args) {
Point3D p1 = new Point3D(1, 3, 4);
Point3D p2 = new Point3D(); //调用这个public Point3D()没有参数 只是初始值
System.out.printf("p1: (%d, %d, %d) \n",
p1.getX(), p1.getY(), p1.getZ());
System.out.printf("p2: (%d, %d, %d) \n",
p2.getX(), p2.getY(), p2.getZ());
}
}
执行结果;
p1: (1, 3, 4)
p2: (0, 0, 0)
收保护的成员(Protected) 有限制的调用
(包括可以被子类调用)
public class Rectangle {
//受保护的member可以被子类调用
protected int x;
protected int y;
protected int width;
protected int height;
public Rectangle() {
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; }
public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public int getArea() { return width*height; }
}
public class Cubic extends Rectangle {
protected int z;
protected int length;
public Cubic() {
super();
}
public Cubic(int x, int y, int z, int length, int width, int height) {
super(x, y, width, height);
this.z = z;
this.length = length;
}
public void setZ(int z) { this.z = z; }
public void setLength(int length) { this.length = length; }
public int getZ() { return z; }
public int getLength() { return length; }
public int getVolumn() { // 可以直接使用父类中的width、height成员
return length*width*height;
}
}
public class ExtendCubic {
public static void main(String[] args) {
Cubic p1 = new Cubic(1,3,4,5,7,9); //int x, int y, int z, int length, int width, int height
Cubic p2 = new Cubic(); //调用这个public Cubic() 没有参数 只是初始值
System.out.printf("p1: (%d,%d) \n",
p1.getVolumn(), p1.getArea());
System.out.printf("p2: (%d) \n",
p2.getVolumn());
}
}
重写的方法(override)方法
1. 重写 返回类型相同
public class SimpleArray{
protected int[] array;
public SimpleArray(int i){
array = new int[i];
}
public void setElement(int i,int data){
array[i]=data;
}
}
扩展类重写方法
public class SafeArray extends SimpleArray{
public SafeArray(int i){
super(i);
}
//扩展类重写方法但接口与simpleArray的 接口相同
public void setElement(int i,int data){
if(i<array.length)
super.setElement(i,data);
}
}
2. 重写 返回类型不同
public class Point2D{
projected int x;
projected int y;
public Point2D(int x,int y)
{
this.x=x;
this.y=y;
}
public Point2D getCopyofMe()
{
return new Point2D(x,y);
}
}
public class Point3D extends Point2D{
protected int z;
public Point3D(int x,int y,int z){
super(x,y);
this.z=z;
//返回不同类型的返回值
public Point3D getCopyOfMe(){
return new Point3D(x,y,z);
}
}
}
OBJECT 是所有类的父类
public class SimpleCollectionDemo {
public static void main(String[] args) {
SimpleCollection simpleCollection = new SimpleCollection();
simpleCollection.add(new Foo1("一号 Foo1"));
simpleCollection.add(new Foo2("一号 Foo2"));
Foo<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="True" sourcevalue="1" unitname="F"><span lang="EN-US" style="mso-bidi-font-size: 10.5pt">1 f</span></chmetcnv>1 = (Foo1) simpleCollection.get(0);
f1.showName();
Foo<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="True" sourcevalue="2" unitname="F"><span lang="EN-US" style="mso-bidi-font-size: 10.5pt">2 f</span></chmetcnv>2 = (Foo2) simpleCollection.get(1);
f2.showName();
}
}
public class SimpleCollection {
private Object[] objArr;
private int index = 0;
public SimpleCollection() {
// 预设10个对象空间
objArr = new Object[10];
}
public SimpleCollection(int capacity) {
objArr = new Object[capacity];
}
public void add(Object o) {
objArr[index] = o;
index++;
}
public int getLength() {
return index;
}
public Object get(int i) {
return objArr[i];
}
}
public class Foo1 {
private String name;
public Foo1(String name) {
this.name = name;
}
public void showName() {
System.out.println("foo1 嘿" + name);
}
}
public class Foo2 {
private String name;
public Foo2(String name) {
this.name = name;
}
public void showName() {
System.out.println("foo2 嘿" + name);
}
}
运行结果
foo1 嘿一号 Foo1
foo2 嘿一号 Foo2
object 中的一些方法
toString()、equals() 和hashCode()方法
public class ToStringDemo {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder();//StringBuilder一个连接两个字符串的类"+"类似
for(int i = 0; i < 10; i++)
builder.append(i);
System.out.println(builder.toString());//toString对对象的String描述,他返回sting实例
}
}
equals()比较对象的内存地址是否相同。
clone()如何复制对象本身的方法。
public class Point implements Cloneable {//定义Point类,这个类可以复制自己
private int x;
private int y;
public Point() {}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public int getX() { return x; }
public int getY() { return y; }
//这个类可以复制自己
public Object clone() throws CloneNotSupportedException {
// 呼叫父类的clone()来进行复制
return super.clone();
}
}
public class Table implements Cloneable {// 要操作Cloneable
private Point center;
public void setCenter(Point center) {//定义了Point为Table类的数据成员之一
this.center = center;
}
public Point getCenter() {
return center;
}
public Object clone ()
throws CloneNotSupportedException {
// 呼叫父类的clone()来复制
Table table = (Table) super.clone();
if(this.center != null) {
// 复制Point类型的数据成员
table.center = (Point) center.clone();
}
return table;
}
}
public class CloneDemo {
public static void main(String[] args)
throws CloneNotSupportedException {
Table table = new Table();
table.setCenter(new Point(2, 3));
Point originalCenter = table.getCenter();
Table clonedTable = (Table) table.clone();
Point clonedCenter = clonedTable.getCenter();
System.out.printf("原来的Table中心:(%d, %d)\n",
originalCenter.getX(), originalCenter.getY());
System.out.printf("复制的Table中心:(%d, %d)\n",
clonedCenter.getX(), clonedCenter.getY());
clonedCenter.setX(10);
clonedCenter.setY(10);
// 改变复制品的内容,对原来的对象不会有影响
System.out.printf("原来的Table中心:(%d, %d)\n",
originalCenter.getX(), originalCenter.getY());
System.out.printf("复制的Table中心:(%d, %d)\n",
clonedCenter.getX(), clonedCenter.getY());
}
}
final 关键字
final声明变量,变量的值不能在变(相当于常量)
final定义方法时,则表示该方法成员再也无法被子类重写(override)
如果是在类上加上final关键字的话,类终止扩展,这个类不能被其他类继承。
多态