本文主要作为java学习笔记,方便以后查看,大部分内容都源于以下网站:

  http://www.ntu.edu.sg/home/ehchua/programming/index.html#Game

本文的内容是根据自己理解而归类,有不准确的地方,敬请谅解。

本文主要内容:

  1.继承(inheritance)

  2.多态(polymorphism)

  3.抽象类(abstract_class)

  4.接口(interface)

-----------------------------------------------------------------

1.继承

修饰符:extends

继承只能是单继承,一个子类只能继承一个父类,不能继承多个父类。

The "@Override" is known as annotation (introduced in JDK 1.5), which asks compiler to check whether there is such a method in the superclass to be overridden. This helps greatly if you misspell the name of the method to be overridden. For example, suppose that you wish to override method toString() in a subclass. If @Override is not used and toString() is misspelled as TOString(), it will be treated as a new method in the subclass, instead of overriding the superclass. If @Override is used, the compiler will signal an error.@Override annotation is optional, but certainly nice to have.
Annotations are not programming constructs. They have no effect on the program output. It is only used by the compiler, discarded after compilation, and not used by the runtime.

“@Override”作为标注,告诉编译器去检查父类中是否存在子类将要重载的同名函数。注意这里的Override开头的‘O’需要大写。

Point.java

public class Point {
private int x;
private int y; public Point() {
x = 0;
y = 0;
}
public Point(int x, int y) {
this.x = x;
this.y = y;
} public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
} public String toString() {
return "(" + x + ", " + y + ")";
}
}

Point3D.java

public class Point3D extends Point {
private int z; public Point3D() {
super();    //调用父类的构造函数
z = 0;
}
public Point3D(int x, int y, int z) {
super(x, y);   //调用父类的构造函数
this.z = z;
} public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
} @Override
public String toString() {
return "(" + super.getX() + ", " + super.getY() + ", " + z + ")";
}
}

TestPoint3D.java

运行结果:

(1, 2, 3)
1
2
3
(4, 5, 6)

Java provides a binary operator called instanceof which returns true if an object is an instance of a particular class. The syntax is as follows:
anObject instanceof aClass

Circle c1 = new Circle();
System.out.println(c1 instanceof Circle); // true

A subclass instance processes all the attributes operations of its superclass. When a superclass instance is expected, it can be substituted by a subclass instance. In other words, a reference to a class may hold an instance of that class or an instance of one of its subclasses - it is called substitutability.
    If a subclass instance is assign to a superclass reference, you can invoke the methods defined in the superclass only. You cannot invoke methods defined in the subclass.
    However, the substituted instance retains its own identity in terms of overridden methods and hiding variables. If the subclass overrides methods in the superclass, the subclass's version will be executed, instead of the superclass's version.

2.多态

Shape.java

public class Shape {
private String color; public Shape (String color) {
this.color = color;
} @Override
public String toString() {
return "Shape of color=\"" + color + "\"";
}
public double getArea() {
System.out.println("shape unknown! Cannot compute area!");
return 0;
}
}

Rectangle.java

public class Rectangle extends Shape {
private int length;
private int width; public Rectangle(String color, int length, int width) {
super(color);
this.length = length;
this.width = width;
}
  //重载父类的方法
@Override
public String toString() {
return "Rectangle of length=" + length + " and width="
+ width + ", subclass of " + super.toString();
}
  //重载父类的方法
@Override
public double getArea() {
return length * width;
}
}

Triangle.java

public class Triangle extends Shape {
private int length;
private int width; public Triangle(String color, int length, int width) {
super(color);
this.length = length;
this.width = width;
}
  //重载父类的方法
@Override
public String toString() {
return "Rectangle of length=" + length + " and width="
+ width + ", subclass of " + super.toString(); }
  //重载父类的方法
@Override
public double getArea() {
return 0.5 * length * width;
}
}

TestShape.java

public class TestShape {
public static void main(String[] args) {
Shape s1 = new Rectangle("red", 4, 5);     //多态的体现,可以将子类复制给父类,应为子类具有父类的属性
System.out.println(s1);
System.out.println("Area is " + s1.getArea());  //根据赋值前的情况,将会调用Rectangle中的getArea函数 Shape s2 = new Triangle("blue", 4, 5);
System.out.println(s2);
System.out.println("Area is " + s2.getArea());  //根据赋值前的情况,将会调用Rectangle中的getArea函数
}
}

运行结果:

Rectangle of length=4 and width=5, subclass of Shape of color="red"
Area is 20.0
Rectangle of length=4 and width=5, subclass of Shape of color="blue"
Area is 10.0

3.抽象类(abastract class)

修饰符:abstract

抽象类只有接口,但是其中的函数并没有实现。

An abstract class is incomplete in its definition, since the implementation of its abstract methods is missing. Therefore, an abstract class cannot be instantiated. In other words, you cannot create instances from an abstract class (otherwise, you will have an incomplete instance with missing method's body).

To use an abstract class, you have to derive a subclass from the abstract class. In the derived subclass, you have to override the abstract methods and provide implementation to all the abstract methods. The subclass derived is now complete, and can be instantiated. (If a subclass does not provide implementation to all the abstract methods of the superclass, the subclass remains abstract.)

This property of the abstract class solves our earlier problem. In other words, you can create instances of the subclasses such as Triangle and Rectangle, and upcast them to Shape (so as to program and operate at the interface level), but you cannot create instance of Shape, which avoid the pitfall that we have faced. For example,

Shape.java

abstract public class Shape {
private String color; public Shape(String color) {
this.color = color;
} @Override
public String toString() {
return "Shape of color\"" + color + "\"";
}
  //其中的方法不必实现,实现由其子类来完成
abstract public double getArea();
}

Rectangle.java和Triangle.java使用上面的。子类继承父类,并实现器中的未实现的函数。抽象类不能够实例化,即通过new操作符,新建一个instance。

TestShape.java

public class TestShape {
public static void main(String[] args) {
Shape s1 = new Rectangle("red", 4, 5);
System.out.println(s1);
System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("blue", 4, 5);
System.out.println(s2);
System.out.println("Area is " + s2.getArea()); //wrong
//Shape s3 = new Shape("green");
}
}

4.接口(interface)

A Java interface is a 100% abstract superclass which define a set of methods its subclasses must support. An interface contains only public abstract methods (methods with signature and no implementation) and possibly constants (public static final variables). You have to use the keyword "interface" to define an interface (instead of keyword "class" for normal classes). The keyword public and abstract are not needed for its abstract methods as they are mandatory.

An interface is a contract for what the classes can do. It, however, does not specify how the classes should do it.

Movable.java

public interface Movable {
public void moveUp();
public void moveDown();
public void moveLeft();
public void moveRight();
}

MovablePoint.java

public class MovablePoint implements Movable {
private int x, y; public MovablePoint(int x, int y) {
this.x = x;
this.y = y;
} @Override
public String toString() {
return "Point at (" + x + ", " + y + ")";
}
@Override
public void moveUp() {
y++;
}
@Override
public void moveDown() {
y--;
}
@Override
public void moveLeft() {
x--;
}
@Override
public void moveRight() {
x++;
}
}

TestMovable.java

public class TestMovable {
public static void main(String[] args) {
Movable m1 = new MovablePoint(7,7); m1.moveUp();
System.out.println(m1);
m1.moveDown();
System.out.println(m1);
m1.moveLeft();
System.out.println(m1);
m1.moveDown();
System.out.println(m1);
}
}

运行结果

Point at (7, 8)
Point at (7, 7)
Point at (6, 7)
Point at (6, 6)

接口的出现,使得java可以间接实现多继承,因为一个类可以通过修饰符implements来实现多个接口。

As mentioned, Java supports only single inheritance. That is, a subclass can be derived from one and only one superclass. Java does not support multiple inheritance to avoid inheriting conflicting properties from multiple superclasses. Multiple inheritance, however, does have its place in programming.

A subclass, however, can implement more than one interfaces. This is permitted in Java as an interface merely defines the abstract methods without the actual implementations and less likely leads to inheriting conflicting properties from multiple interfaces. In other words, Java indirectly supports multiple inheritances via implementing multiple interfaces. For example,

public class Circle extends Shape implements Movable, Displayable {  // One superclass but implement multiple interfaces
.......
}

The formal syntax for declaring interface is:

[public|protected|package] interface interfaceName
[extends superInterfaceName] {
// constants
static final ...; // abstract methods' signature
...
}

oop-Inheritance & Polymorphism的更多相关文章

  1. 封装,capsulation,&&继承,Inheritance,&&多态,polymorphism

    Inheritance&&polymorphism 层次概念是计算机的重要概念.通过继承(inheritance)的机制可对类(class)分层,提供类型/子类型的关系.C++通过类派 ...

  2. 【转】Basic C# OOP Concept

    This Article will explain a very simple way to understand the basic C# OOP Concept Download ShanuBas ...

  3. systemverilog之OOP

    what is oop terminology an example class default methods for classes static attibute assigment and c ...

  4. JAVA面向对象初步知识总结:封装、继承、多态

    1.封装 把数据和方法包装进类中,以及具体实现的隐藏,常共同被称作是是封装.其结果是一个同时带有特征和行为的数据类型.所谓具体实现的隐藏是通过访问权限控制实现的.JAVA 子类重写继承的方法时,不可以 ...

  5. Design Pattern: Not Just Mixin Pattern

    Brief 从Mix-In模式到Mixin模式,中文常用翻译为“混入/织入模式”.单纯从名字上看不到多少端倪,而通过采用Mixin模式的jQuery.extend我们是否可以认为Mixin模式就是深拷 ...

  6. 115 Java Interview Questions and Answers – The ULTIMATE List--reference

    In this tutorial we will discuss about different types of questions that can be used in a Java inter ...

  7. Service Oriented Architecture and WCF 【转】

    http://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF Introduction This a ...

  8. Java Knowledge series 4

    JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...

  9. Java Knowledge series 3

    JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...

  10. Introduction to Object-Oriented JavaScript 转载自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

    Introduction to Object-Oriented JavaScript IN THIS ARTICLE JavaScript review Object-oriented program ...

随机推荐

  1. Linux时间子系统(二) 软件架构

    一.前言 本文的主要内容是描述内核时间子系统的软件框架.首先介绍了从旧的时间子系统迁移到新的时间子系统的源由,介绍新的时间子系统的优势.第三章汇整了时间子系统的相关文件以及内核配置.最后描述各种内核配 ...

  2. Python2 unichr() 函数

    描述 unichr() 函数 和 chr()函数功能基本一样, 只不过是返回 unicode 的字符. 语法 以下是 unichr() 方法的语法: unichr(i) 参数 i -- 可以是10进制 ...

  3. python标准库介绍——14 gc 模块详解

    ==gc 模块== (可选, 2.0 及以后版本) ``gc`` 模块提供了到内建循环垃圾收集器的接口. Python 使用引用记数来跟踪什么时候销毁一个对象; 一个对象的最后一个引用一旦消失, 这个 ...

  4. mysql添加和root用户一样的权限

    以下语句具有和ROOT用户一样的权限.大家在拿站时应该碰到过.root用户的mysql,只可以本地连,对外拒绝连接.以下方法可以帮助你解决这个问题了,下面的语句功能是,建立一个用户为monitor密码 ...

  5. [k8s]通过svc来访问集群podhttp://api:8080/api/v1/namespaces/default/services/mynginx/proxy/

    以往的套路是,安装完成k8s集群后-->安装dns-->安装ingress以便外面访问(或者映射nodeport方式) 如果你不想做任何关于ingress/nodeport一些操作.想通过 ...

  6. UVA10519 - !! Really Strange !!(数论+高精度)

    10519 - !! Really Strange !!(数论+高精度) option=com_onlinejudge&Itemid=8&category=24&page=sh ...

  7. uva--242(邮资问题 dp)

    输入输出: id=26127" style="color:blue; text-decoration:none">Sample Input 5 2 4 1 4 12 ...

  8. Zookeeper运维的一些经验[转]

    Zookeeper是一个分布式协调框架,有不错的性能,也经过许多公司的验证,所以在很多场景都有使用.大家一般用Zookeeper来实现服务发现(类似DNS),配置管理,分布式锁,leader选举等.在 ...

  9. spark内存概述

    转自:https://github.com/jacksu/utils4s/blob/master/spark-knowledge/md/spark%E5%86%85%E5%AD%98%E6%A6%82 ...

  10. perl模块安装——cpanm

    下载 wget  http://xrl.us/cpanm mv cpanm.1 cpanm 配置 在bashrc或zshrc里面加入下面的几句话 最好把相对路径改成绝度路径(将~换成你根目录的绝对路径 ...