「java面向继承」Java关于继承

博主:adminadmin 2023-01-09 16:12:10 882

今天给各位分享java面向继承的知识,其中也会对Java关于继承进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java面向对象继承

package wenwen; public class Wiring { protected double power;//功率 protected double pressure;//额定电压 protected double current;//额定电流 protected String electricity;//直流类型 //创建电器类的构造函数 public Wiring(double power,double pressure,double current, String electricity) { super(); this.power = power; this.pressure = pressure; this.current = current; this.electricity = electricity; } //创建work函数 public void work(){ System.out.println("电器工作功率是:"+power+"电器额定电压是:"+pressure+"电器额定电流是:"+current+"电器工作电流类型是:"+electricity); } public double getPower() { return power; } public void setPower(double power) { this.power = power; } public double getPressure() { return pressure; } public void setPressure(double pressure) { this.pressure = pressure; } public double getCurrent() { return current; } public void setCurrent(double current) { this.current = current; } public String getElectricity() { return electricity; } public void setElectricity(String electricity) { this.electricity = electricity; } } /*电视类继承父类wiring*/ public class TV extends Wiring{ protected String type;//电视类型 protected int voulme;//电视最大音量 //创建电视类的构造函数 public TV(double power, double pressure, double current, String electricity, String type, int voulme) { super(power, pressure, current, electricity); this.type = type; this.voulme = voulme; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getVoulme() { return voulme; } public void setVoulme(int voulme) { this.voulme = voulme; } //对work函数的重写 public void work(){ System.out.println("电视工作功率是:"+power+"电视额定电压是:"+pressure+"电视额定电流是:"+current+"电视工作电流类型是:"+electricity+"电视的型号是:"+type+"电视的最大音量是:"+voulme); //System.out.println("电视的型号是:"+type+"电视的最大音量是:"+voulme); } } /*冰箱类继承父类wiring*/ public class Icebox extends Wiring{ protected double capacity;//容量 //创建冰箱类的构造函数 public Icebox(double power, double pressure, double current, String electricity, double capacity) { super(power, pressure, current, electricity); this.capacity = capacity; } public double getCapacity() { return capacity; } public void setCapacity(double capacity) { this.capacity = capacity; } //继承work工作方法 public void work(){ System.out.println("冰箱工作功率是:"+power+"冰箱额定电压是:"+pressure+"冰箱额定电流是:"+current+"冰箱工作电流类型是:"+electricity); } } 这是我写的有不好的地方给点意见(java我们也刚学),如果刚觉还可以,请采纳,谢谢!

java中的继承是什么

Java继承是面向对象的最显著的一个特征。继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。[1]

Java继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性地继承父类。这种技术使得复用以前的代码非常容易,能够大大缩短开发周期,降低开发费用。比如可以分隔符先定义一个类叫车,车有以下属性:车体大小,颜色,方向盘,轮胎,而又由车这个类派生出轿车和卡车两个类,为轿车添加一个小后备箱,而为卡车添加一个大货箱。

类和类之间的继承关系可以用UML符号表示,其中父类又叫超类或基类,子类又叫派生类。父类是子类的一般化,子类是父类的特化(具体化)。

JAVA不支持多继承,单继承使JAVA的继承关系很简单,一个类只能有一个父类,易于管理程序,同时一个类可以实现多个接口,从而克服单继承的缺点。

在面向对象程序设计中运用继承原则,就是在每个由一般类和特殊类形成的一般——特殊结构中,把一般类的对象实例和所有特殊类的对象实例都共同具有的属性和操作一次性地在一般类中进行显式的定义,在特殊类中不再重复地定义一般类中已经定义的东西,但是在语义上,特殊类却自动地、隐含地拥有它的一般类(以及所有更上层的一般类)中定义的属性和操作。特殊类的对象拥有其一般类的全部或部分属性与方法,称作特殊类对一般类的继承。

继承所表达的就是一种对象类之间的相交关系,它使得某类对象可以继承另外一类对象的数据成员和成员方法。若类B继承类A,则属于B的对象便具有类A的全部或部分性质(数据属性)和功能(操作),我们称被继承的类A为基类、父类或超类,而称继承类B为A的派生类或子类。

继承避免了对一般类和特殊类之间共同特征进行的重复描述。同时,通过继承可以清晰地表达每一项共同特征所适应的概念范围——在一般类中定义的属性和操作适应于这个类本身以及它以下的每一层特殊类的全部对象。运用继承原则使得系统模型比较简练也比较清晰。

举例说明java面向对象的封装继承多态

继承就是在面向对象中体现的是is-a

的关系,是一个,封装就是写一个类了,多态则体现的是一个行为的多样性

使用java继承机制编程有什么好处谈谈你的实践体会

更安全。

继承简化了人们对事物的认识和描述,能清晰体现相关类间的层次结构关系。继承提供了软件复用功能。这种做法能减小代码和数据的冗余度,大大增加程序的重用性。提供多重继承机制。出于安全性和可靠性的考虑,仅支持单重继承,而通过使用接口机制来实现多重继承。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

java继承

继承是面向对象编程技术的一块基石,因为它允许创建分等级层次的类。运用继承,你能够创建一个通用类,它定义了一系列相关项目的一般特性。该类可以被更具体的类继承,每个具体的类都增加一些自己特有的东西。在Java 术语学中,被继承的类叫超类(superclass ),继承超类的类叫子类(subclass )。因此,子类是超类的一个专门用途的版本,它继承了超类定义的所有实例变量和方法,并且为它自己增添了独特的元素。

继承一个类,只要用extends 关键字把一个类的定义合并到另一个中就可以了。为了理解怎样继承,让我们从简短的程序开始。下面的例子创建了一个超类A和一个名为B的子类。注意怎样用关键字extends 来创建A的一个子类。

// A simple example of inheritance.

// Create a superclass.

class A {

int i, j;

void showij() {

System.out.println("i and j: " + i + " " + j);

}

}

class B extends A {

int k;

void showk() {

System.out.println("k: " + k);

}

void sum() {

System.out.println("i+j+k: " + (i+j+k));

}

}

class SimpleInheritance {

public static void main(String args[]) {

A superOb = new A();

B subOb = new B();

System.out.println("Contents of superOb: ");

superOb.showij();

System.out.println();

subOb.i = 7;

subOb.j = 8;

subOb.k = 9;

System.out.println("Contents of subOb: ");

subOb.showij();

subOb.showk();

System.out.println();

System.out.println("Sum of i, j and k in subOb:");

subOb.sum();

}

}

该程序的输出如下:

Contents of superOb:

i and j: 10 20

Contents of subOb:

i and j: 7 8

k: 9

Sum of i, j and k in subOb:

i+j+k: 24

像你所看到的,子类B包括它的超类A中的所有成员。这是为什么subOb 可以获取i和j 以及调用showij( ) 方法的原因。同样,sum( ) 内部,i和j可以被直接引用,就像它们是B的一部分。

尽管A是B的超类,它也是一个完全独立的类。作为一个子类的超类并不意味着超类不能被自己使用。而且,一个子类可以是另一个类的超类。声明一个继承超类的类的通常形式如下:

class subclass-name extends superclass-name {

// body of class

}

你只能给你所创建的每个子类定义一个超类。Java 不支持多超类的继承(这与C++ 不同,在C++中,你可以继承多个基础类)。你可以按照规定创建一个继承的层次。该层次中,一个子类成为另一个子类的超类。然而,没有类可以成为它自己的超类。

成员的访问和继承

尽管子类包括超类的所有成员,它不能访问超类中被声明成private 的成员。例如,考虑下面简单的类层次结构:

/* In a class hierarchy, private members remain private to their class.

This program contains an error and will not compile.

*/

// Create a superclass.

class A {

int i;

private int j; // private to A

void setij(int x, int y) {

i = x; j = y;

}

}

// A"s j is not accessible here.

class B extends A {

int total; void sum() {

total = i + j; // ERROR, j is not accessible here

}

}

class Access {

public static void main(String args[]) {

B subOb = new B();

subOb.setij(10, 12);

subOb.sum();

System.out.println("Total is " + subOb.total);

}

}

该程序不会编译,因为B中sum( ) 方法内部对j的引用是不合法的。既然j被声明成private,它只能被它自己类中的其他成员访问。子类没权访问它。

注意:一个被定义成private 的类成员为此类私有,它不能被该类外的所有代码访问,包括子类。

更实际的例子

让我们看一个更实际的例子,该例子有助于阐述继承的作用。新的类将包含一个盒子的宽度、高度、深度。

// This program uses inheritance to extend Box.

class Box {

double width; double height; double depth;

// construct clone of an object

Box(Box ob) { // pass object to constructor

width = ob.width;

height = ob.height;

depth = ob.depth;

}

// constructor used when all dimensions specified

Box(double w, double h, double d) {

width = w;

height = h;

depth = d;

}

// constructor used when no dimensions specified

Box() {

width = -1; // use -1 to indicate

height = -1; // an uninitialized

depth = -1; // box

}

// constructor used when cube is created

Box(double len) {

width = height = depth = len;

}

// compute and return volume double

volume() {

return width * height * depth;

}

}

BoxWeight extends Box {

double weight; // weight of box

// constructor for BoxWeight

BoxWeight(double w, double h, double d, double m) {

width = w;

height = h;

depth = d;

weight = m;

}

}

class DemoBoxWeight {

public static void main(String args[]) {

BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);

BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);

double vol;

vol = mybox1.volume();

System.out.println("Volume of mybox1 is " + vol);

System.out.println("Weight of mybox1 is " + mybox1.weight);

System.out.println();

vol = mybox2.volume();

System.out.println("Volume of mybox2 is " + vol);

System.out.println("Weight of mybox2 is " + mybox2.weight);

}

}

该程序的输出显示如下:

Volume of mybox1 is 3000.0

Weight of mybox1 is 34.3

Volume of mybox2 is 24.0

Weight of mybox2 is 0.076

BoxWeight 继承了Box 的所有特征并为自己增添了一个weight 成员。没有必要让BoxWeight 重新创建Box 中的所有特征。为满足需要我们只要扩展Box就可以了。

继承的一个主要优势在于一旦你已经创建了一个超类,而该超类定义了适用于一组对象的属性,它可用来创建任何数量的说明更多细节的子类。每一个子类能够正好制作它自己的分类。例如,下面的类继承了Box并增加了一个颜色属性:

// Here, Box is extended to include color.

class ColorBox extends Box {

int color; // color of box

ColorBox(double w, double h, double d, int c) {

width = w;

height = h;

depth = d;

color = c;

}

}

记住,一旦你已经创建了一个定义了对象一般属性的超类,该超类可以被继承以生成特殊用途的类。每一个子类只增添它自己独特的属性。这是继承的本质。

超类变量可以引用子类对象

超类的一个引用变量可以被任何从该超类派生的子类的引用赋值。你将发现继承的这个方面在很多条件下是很有用的。例如,考虑下面的程序:

class RefDemo {

public static void main(String args[]) {

BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);

Box plainbox = new Box(); double vol;

vol = weightbox.volume();

System.out.println("Volume of weightbox is " + vol);

System.out.println("Weight of weightbox is " + weightbox.weight);

System.out.println();

// assign BoxWeight reference to Box reference

plainbox = weightbox;

vol = plainbox.volume(); // OK, volume() defined in Box

System.out.println("Volume of plainbox is " + vol);

/* The following statement is invalid because plainbox does not define a weight member. */

// System.out.println("Weight of plainbox is " + plainbox.weight);

}

}

这里,weightbox 是BoxWeight 对象的一个引用,plainbox 是Box对象的一个引用。既然BoxWeight 是Box的一个子类,允许用一个weightbox 对象的引用给plainbox 赋值。

当一个子类对象的引用被赋给一个超类引用变量时,你只能访问超类定义的对象的那一部分。这是为什么plainbox 不能访问weight 的原因,甚至是它引用了一个BoxWeight 对象也不行。仔细想一想,这是有道理的,因为超类不知道子类增加的属性。这就是本程序中的最后一行被注释掉的原因。Box的引用访问weight 域是不可能的,因为它没有定义。

是否可以解决您的问题?

java面向对象关于继承的问题

Base base=new Child();

这个分为两部分。。Base base这是定义。。base=new Child();这是赋值。。

因为你把base定义为 Base类型。。所以编译器把他当做Base类型。。至于实际类型。。运行的时候才能确定。。所以不能访问Child的属性和方法。。

Child child=new Child();

Base child=new Child();

这个的不同应该明白了撒。。就是你写代码的时候。。编译器把他们看成的类型是不一样的。。

java面向继承的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java关于继承、java面向继承的信息别忘了在本站进行查找喔。