「java语句继承」java中继承的用法

博主:adminadmin 2023-03-18 14:07:10 487

本篇文章给大家谈谈java语句继承,以及java中继承的用法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中继承的语法格式是怎样的

//父类 Shape类的声明

abstract class Shape

{

public final double PI = 3.141592654;

protected Color color; //颜色

protected int lineSize; //线宽

public Shape() //构造函数1

{

color = Color.black;

lineSize = 1;

}

public Shape(Color c, int ls) //构造函数2

{

color = c;

lineSize = ls;

}

//设置颜色方法

public void setColor(Color c)

{

color = c;

}

//获得颜色方法

public Color getColor()

{

return color;

}

//设置线宽方法

public void setLineSize(int ls)

{

lineSize = ls;

}

//获得线宽方法

public int getLineSize()

{

return lineSize;

}

//抽象方法area

abstract double area();

//抽象方法perimeter

abstract double perimeter();

}

//子类Circle的声明

class Circle extends Shape

{

int centerX; //圆心X坐标

int centerY; //圆心Y坐标

int radius; //圆的半? //构造函数

public Circle(int x, int y, int r)

{

super(); //使用父类的构造函数构建父类

centerX = x;

centerY = y;

radius = r;

}

public double area()

{

return (int)(PI * radius * radius );

}

public double perimeter()

{

return (int)(2*PI*radius);

}

}

//子类Rectangle声明

class Rectangle extends Shape

{

int left; //矩形左上角X坐标

int top; //矩形左上角Y坐标

int width; //矩形长度

int height; //矩形宽度

//构造函数

public Rectangle(int l, int t, int w, int h)

{

super();

left = l;

top = t;

width = w;

height = h;

}

public double area()

{

return (int)(width * height);

}

public double perimeter()

{

return (int)((width + height)*2);

}

}

//子类RectangleEx声明

class RectangleEx extends Rectangle

{

int radius; // 圆角半径?

//构造函数

public RectangleEx(int l, int t, int w, int h, int r)

{

super(l, t, w, h); //用父类构造函数构建父类

radius = r;

}

public double area()

{

return (int)(super.area() - (4 - PI) * radius* radius);

}

public double perimeter()

{

return (int)((width + height)*2-8*radius+2*PI*radius);

}

}

public class extend

{

public static void main(String args[])

{

//创建三个Shape对象

Shape shape[] = new Shape[3];

shape[0] = new Circle(50, 50, 40);

shape[1] = new Rectangle(0, 0, 40, 30);

shape[2] = new RectangleEx(20, 30, 40, 30, 5);

//创建三个Shape对象的信息数组

String ShapeName[]=new String[3];

ShapeName[0]="圆Circle(50, 50, 40)";

ShapeName[1]="矩形Rectangle(0, 0, 40, 30)";

ShapeName[2]="圆角矩形RectangleEx(20, 30, 40, 30, 5)";

//求面积和周长

for(int i = 0; i shape.length; i++)

{

System.out.println(ShapeName[i]+"的面积="+ shape[i].area());

System.out.println(ShapeName[i]+"的周长="+shape[i].perimeter());

System.out.println();

}

}

}

java语言中的重写、重载、继承?

重写,英文名是override,是指在继承情况下,子类中定义了与其基类中方法具有相同型构的新方法,就叫做子类把基类的方法重写了。这是实现多态必须的步骤。

重载,英文名是overload,是指在同一个类中定义了一个以上具有相同名称,但是型构不同的方法。在同一个类中,是不允许定义多于一个的具有相同型构的方法的。

Java实现继承

js继承有5种实现方式:

1、继承第一种方式:对象冒充

function Parent(username){

this.username = username;

this.hello = function(){

alert(this.username);

}

}

function Child(username,password){

//通过以下3行实现将Parent的属性和方法追加到Child中,从而实现继承

//第一步:this.method是作为一个临时的属性,并且指向Parent所指向的对象,

//第二步:执行this.method方法,即执行Parent所指向的对象函数

//第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法

this.method = Parent;

this.method(username);//最关键的一行

delete this.method;

this.password = password;

this.world = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

var child = new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

2、继承第二种方式:call()方法方式

call方法是Function类中的方法

call方法的第一个参数的值赋值给类(即方法)中出现的this

call方法的第二个参数开始依次赋值给类(即方法)所接受的参数

function test(str){

alert(this.name + " " + str);

}

var object = new Object();

object.name = "zhangsan";

test.call(object,"langsin");//此时,第一个参数值object传递给了test类(即方法)中出现的this,而第二个参数"langsin"则赋值给了test类(即方法)的str

function Parent(username){

this.username = username;

this.hello = function(){

alert(this.username);

}

}

function Child(username,password){

Parent.call(this,username);

this.password = password;

this.world = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

var child = new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

3、继承的第三种方式:apply()方法方式

apply方法接受2个参数,

A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this

B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数

function Parent(username){

this.username = username;

this.hello = function(){

alert(this.username);

}

}

function Child(username,password){

Parent.apply(this,new Array(username));

this.password = password;

this.world = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

var child = new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承

function Person(){

}

Person.prototype.hello = "hello";

Person.prototype.sayHello = function(){

alert(this.hello);

}

function Child(){

}

Child.prototype = new Person();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承

Child.prototype.world = "world";

Child.prototype.sayWorld = function(){

alert(this.world);

}

var c = new Child();

c.sayHello();

c.sayWorld();

5、继承的第五种方式:混合方式

混合了call方式、原型链方式

function Parent(hello){

this.hello = hello;

}

Parent.prototype.sayHello = function(){

alert(this.hello);

}

function Child(hello,world){

Parent.call(this,hello);//将父类的属性继承过来

this.world = world;//新增一些属性

}

Child.prototype = new Parent();//将父类的方法继承过来

Child.prototype.sayWorld = function(){//新增一些方法

alert(this.world);

}

var c = new Child("zhangsan","lisi");

c.sayHello();

c.sayWorld();

java语言的继承

首先更正一下楼主的错误认识,用implements 实现多接口叫“实现”,不叫“继承”这是两个概念。java是单继承的,通过实现多接口达到了多继承的目的。

而接口继承接口才能叫继承,也可以多继承。

(而接口里又没有方法名 ?????)

谁告诉你没有方法名的?那还要接口有什么用?只不过是没有方法的实现而已。

Runnable接口里的run()方法不是方法么?

之所以可以“实现多接口”也就是给你提供了编程的灵活性,可以根据自己程序的需要去改造代码。

个人感觉实现接口并不能体现代码重用性,只有继承了某个类可以实现

比如你写了一个类A,这个类实现了B,C接口,而且你根据自己的需要重写了其中的某些方法。

你在写一个类D,D从A继承,这时可以体现重用性。

java语言的类间的继承关系是??

Java语言中的类只支持单继承,而接口支持多继承。

Java中多继承的功能是通过接口(interface)来间接实现的。

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

继承分为单继承和多重继承。单继承是指一个子类最多只能有一个父类。多继承是一个子类可以有二个以上的父类。由于多继承会带来二义性,在实际应用中应尽量使用单继承。

继承是面向对象最显著的一个特性。继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。[1] Java继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性地继承父类。这种技术使得复用以前的代码非常容易,能够大大缩短开发周期,降低开发费用。

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