「java参数继承」java继承方法
本篇文章给大家谈谈java参数继承,以及java继承方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java中子类如何继承父类的含参数的构造方法
- 2、JAVA中继承是怎么回事
- 3、Java实现继承
- 4、java中继承的数值传递引用
- 5、java中子类如何继承有参数的父类方法
- 6、JAVA中,子类将继承父类的所有属性和方法么~?为什么?
java中子类如何继承父类的含参数的构造方法
子类的构造函数中 用super(参数类型 参数);
同时子类的构造函数本身的参数列表要和父类的匹配
JAVA中继承是怎么回事
Java继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性地继承父类。这种技术使得复用以前的代码非常容易,能够大大缩短开发周期,降低开发费用。比如可以先定义一个类叫车,车有以下属性:车体大小,颜色,方向盘,轮胎,而又由车这个类派生出轿车和卡车两个类,为轿车添加一个小后备箱,而为卡车添加一个大货箱。
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中继承的数值传递引用
这个题目如下思路与实现
思路:
依照圆和圆柱的描述抽象成Java程序里的对象Cylinder与Circle,这也是Java的特点之一
圆与圆柱的关系:圆柱继承圆
写测试类TestCylinder
实现:
1.圆:
class Circle {
private double radius;// 半径
// 构造方法半径设为1
public Circle() {
super();
this.radius = 1.0;
}
// 设置半径
public void setRadius(Double radius) {
this.radius = radius;
}
// 得到半径
public Double getRadius() {
return radius;
}
}
2.圆柱
class Cylinder extends Circle {
private double length;
public Cylinder() {
super();
this.length = 1.0;
}
public void setLength(double length) {
this.length = length;
}
public double getLength() {
return length;
}
public double findVolume() {
return this.length * super.getRadius() * super.getRadius() * Math.PI;
}
}
3.测试
public class TestCylinder {
public static void main(String[] args) {
Cylinder cy = new Cylinder();//实例化一个圆柱
System.out.println(cy.findVolume());//因为length有默认值1,圆柱的父类圆具有半径且初始值是1,此处,没有主动给length和radius赋值,所以findVolume里使用的半径与高都是1,所以体积是3.141592653589793
cy.setRadius(2.0);
cy.setLength(2.0);
System.out.println(cy.findVolume());//主动给半径和高设置值,那么cy的半径与高的初始值就会被改变,结果就是2*2*2*PI
}
}
运行结果:
3.141592653589793
25.132741228718345
java中子类如何继承有参数的父类方法
class fathertest{
public fathertest(int i){
System.out.println("hello"+"i");
}
}
public class test extends fathertest {
test(int i){
super(i);
System.out.println("java");
}
public static void main(String[] args) {
test s=new test(0);
}
}
JAVA中,子类将继承父类的所有属性和方法么~?为什么?
是的,子类将继承父类的非私有的属性和方法。
在JAVA中,子类继承父类的所有方法和属性(构造方法要用关键super([参数])调用);继承是JAVA中一个重要的特色,便于程序的编写,提高代码的重用性。
1、如果给子类i提供get和set 通过get调用的自然是子类的。
2、如果给父类和子类分别提供get和set,调的仍然是子类的,因为方法被重写。
扩展资料
在继承中用到super调用父类的构造
private String name;
private String sex;
public xinxin1(String name,String sex)
{
this.name=name;
this.sex=sex;
}
public void hello(){
System.out.println(“嗨!我是”+name+”我是”+sex+”孩”);
}
关于java参数继承和java继承方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。