java是单继承_Java单继承

Java单继承

Java允许类的单继承,接口的多继承,这里先看看类的单继承,可以通过创建几个不同的类继承同一个主类,实现多态。

In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

实验

package org.dc;

import java.util.Arrays;

/**

* java中只允许单继承,所以可以通过构造两个不同的子类对于java的父类进项不同的维度的继承.

*/

class Processor {// 父类中没有出现static方法,所以父类的一切特性都可以被继承

public String name() {

return getClass().getSimpleName();

/*

* Returns the runtime class of this Object. The returned Class object

* is the object that is locked by static synchronized methods of the

* represented class. Returns the simple name of the underlying class as

* given in the source code. Returns an empty string if the underlying

* class is anonymous.

*/}

Object process(Object input) {

return input;

}

}

class Uppercharacter extends Processor {// 继承Processor

@Override

Object process(Object input) {

// 方法的重写构成了多态性,注意这里是重写不是重载!

// 重写的方法在形式(参数类型)上与父类的方法一致,只是内容上有所变化,这体现了java的多态性

return ((String) input).toUpperCase();

/*

* Converts all of the characters in this String to upper case using the

* rules of the default locale

*/

}

}

class Splitregex extends Processor {// 继承Processor

@Override

Object process(Object input) {// 方法的重写构成了多态性

return Arrays.toString(((String) input).split(" "));

/*

* Returns a string representation of the contents of the specified

* array. Splits this string around matches of the given regular

* expression.

*/}

}

public class UsingClassInheritance {

public static void process(Processor p, Object input) {// has-a

// 关系,直接调用已经封装好的Processor对象

/*

* 父类类型的引用可以调用父类中定义的所有属性和方法,而对于子类中定义而父类中没有的方法,它是无可奈何的;

* 同时,父类中的一个方法只有在在父类中定义而在子类中没有重写的情况下,才可以被父类类型的引用调用.

* 对于父类中定义的方法,如果子类中重写了该方法,那么父类类型的引用将会调用子类中的这个方法,这就是动态连接.

*/

System.out.println("The runtime class of this Object is " + p.name());

System.out.println(p.process(input));

}

public static String sentence = "Hello World!";

public static void main(String[] args) {

process(new Uppercharacter(), sentence);

process(new Splitregex(), sentence);

}

}

实验结果

The runtime class of this Object is Uppercharacter

HELLO WORLD!

The runtime class of this Object is Splitregex

[Hello, World!]

继承的目的

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all themembers(fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.                                                                                                                                                                                                    ——Java Documentation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值