不知道你是不是这个意思。public class A extends B { A() { System.out.println("A"); } public static void main(String[] args) { new A(); } } class B extends C { B() { System.out.println("B"); } }class C { C() { System.out.println("C"); } } ================》》 C B A
public class HelloWorld { public static void main(String[] args) { new A().display(); } }class C { int x; public C() { x = 1; }}class B extends C { int y; public B() { super(); y = 2; }}class A extends B { int z; public A() { super(); z = 3; } public void display() { System.out.println(x); System.out.println(y); System.out.println(z); }} 逐层调用父类的构造器. 结果为:1 2 3
可以的,但是调用无参构造,一般打印输出没有明显的结果,如果有参比较明显:
class Person{ //Person类 private String name; private int age; public Person(String name,int age){ //Person的构造方法 this.setName(name); this.setAge(age); } public void setName(String name){ this.name=name; } public void setAge(int age){ this.age=age; } public String getName( ){ return this.name; } public int getAge(){ return this.age; } } class Student extends Person{ //Student类,继承Person类 private float score; //如果调用Person类的构造方法加上 private float score; public Student(String name,int age,float score){ //Student类构造方法 super(name,age); //调用Person类的构造方法 this.setScore(score); } public void setScore(float score){ this.score=score; } public float getScore( ){ return this.score; } } class Pupil extends Student{ //Pupil类,继承Student类 private float height; public Pupil(String name,int age,float score,float height){ //Pupil类构造方法 /*调用Student类的构造方法,也可以调用Person类的代码如下: Super(name,age);*/ super(name,age,score); this.setHeight(height); } /*如果调用Person类的构造方法,则加入score的setter与getter方法: public void setScore(float score){ this.score=score; } public float getScore( ){ return this.score; }但明显代码重复,不建议使用 */ public void setHeight(float height){ this.height=height; } public float getHeight(){ return this.height; } } public class Demo{ public static void main(String args[]){ Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f); //实例化Pupil对象 System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+ "\t成绩:"+p.getScore()+"\t身高:"+p.getHeight()); //打印输出 } }
class Person{ //Person类 private String name; private int age; public Person(String name,int age){ //Person的构造方法 this.setName(name); this.setAge(age); } public void setName(String name){ this.name=name; } public void setAge(int age){ this.age=age; } public String getName( ){ return this.name; } public int getAge(){ return this.age; } } class Student extends Person{ //Student类,继承Person类 private float score; public Student(String name,int age,float score){ //Student类构造方法 super(name,age); //调用Person类的构造方法 this.setScore(score); } public void setScore(float score){ this.score=score; } public float getScore( ){ return this.score; } } class Pupil extends Student{ //Pupil类,继承Student类 private float height; //如果调用Person类的构造方法加上 private float score; public Pupil(String name,int age,float score,float height){ //Pupil类构造方法 /*调用Student类的构造方法,也可以调用Person类的代码如下: Super(name,age);*/ super(name,age,score); this.setHeight(height); } /*如果调用Person类的构造方法,则加入score的setter与getter方法: public void setScore(float score){ this.score=score; } public float getScore( ){ return this.score; }但明显代码重复,不建议使用 */ public void setHeight(float height){ this.height=height; } public float getHeight(){ return this.height; } } public class Demo{ public static void main(String args[]){ Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f); //实例化Pupil对象 System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+ "\t成绩:"+p.getScore()+"\t身高:"+p.getHeight()); //打印输出 } } 嵌入Java代码看起来好些,改了一个小错误://如果调用Person类的构造方法加上 private float score; 加到private float height; 这一行。 希望对楼主有用。
class Person{ //Person类 private String name; private int age; public Person(String name,int age){ //Person的构造方法 this.setName(name); this.setAge(age); } public void setName(String name){ this.name=name; } public void setAge(int age){ this.age=age; } public String getName( ){ return this.name; } public int getAge(){ return this.age; } } class Student extends Person{ //Student类,继承Person类 private float score; public Student(String name,int age,float score){ //Student类构造方法 super(name,age); //调用Person类的构造方法 this.setScore(score); } public void setScore(float score){ this.score=score; } public float getScore( ){ return this.score; } } class Pupil extends Student{ //Pupil类,继承Student类 private float height; //如果调用Person类的构造方法加上 private float score; public Pupil(String name,int age,float score,float height){ //Pupil类构造方法 /*调用Student类的构造方法,也可以调用Person类的代码如下: Super(name,age);*/ super(name,age,score); this.setHeight(height); } /*如果调用Person类的构造方法,则加入score的setter与getter方法: public void setScore(float score){ this.score=score; } public float getScore( ){ return this.score; }但明显代码重复,不建议使用 */ public void setHeight(float height){ this.height=height; } public float getHeight(){ return this.height; } } public class Demo{ public static void main(String args[]){ Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f); //实例化Pupil对象 System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+ "\t成绩:"+p.getScore()+"\t身高:"+p.getHeight()); //打印输出 } }
貌似java不支持多继承,你可以把A定义接口,然后再实现继承
在Java中要实例化子类对象,会自动调用父类的构造方法。
class C { public C() { System.out.println("C中构造方法!"); } }; class B extends C { public B() { System.out.println("B中构造方法!"); } }; public class A extends B { public static void main(String[] args) { new A(); } }; 结果是: C中构造方法! B中构造方法!
A() {
System.out.println("A");
}
public static void main(String[] args) {
new A();
}
}
class B extends C {
B() {
System.out.println("B");
}
}class C {
C() {
System.out.println("C");
}
}
================》》
C
B
A
同样的道理,我想要的是A中也显式的调用C的这个无参数的构造方法。我知道如果在B中显式的用了super()方法,在C的构造方法中也用到了super(),单继承嘛,C其实在实例的时候也会执行A中的对应的构造函数中的内容。这都不是问题的关键,关键是我想知道,能不能在A中也显式地调用C的构造方法,如果能用这样的方式。
同样的道理,我想要的是A中也显式的调用C的这个无参数的构造方法。我知道如果在B中显式的用了super()方法,在C的构造方法中也用到了super(),单继承嘛,C其实在实例的时候也会执行A中的对应的构造函数中的内容。这都不是问题的关键,关键是我想知道,能不能在A中也显式地调用C的构造方法,如果能用这样的方式。
public static void main(String[] args) {
new A().display();
}
}class C {
int x; public C() {
x = 1;
}}class B extends C {
int y; public B() {
super();
y = 2;
}}class A extends B {
int z; public A() {
super();
z = 3;
} public void display() {
System.out.println(x);
System.out.println(y);
System.out.println(z); }}
逐层调用父类的构造器.
结果为:1 2 3
class Person{ //Person类
private String name;
private int age;
public Person(String name,int age){ //Person的构造方法
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName( ){
return this.name;
}
public int getAge(){
return this.age;
}
}
class Student extends Person{ //Student类,继承Person类
private float score; //如果调用Person类的构造方法加上 private float score;
public Student(String name,int age,float score){ //Student类构造方法
super(name,age); //调用Person类的构造方法
this.setScore(score);
}
public void setScore(float score){
this.score=score;
}
public float getScore( ){
return this.score;
}
}
class Pupil extends Student{ //Pupil类,继承Student类
private float height;
public Pupil(String name,int age,float score,float height){ //Pupil类构造方法
/*调用Student类的构造方法,也可以调用Person类的代码如下:
Super(name,age);*/ super(name,age,score);
this.setHeight(height);
}
/*如果调用Person类的构造方法,则加入score的setter与getter方法:
public void setScore(float score){
this.score=score;
}
public float getScore( ){
return this.score;
}但明显代码重复,不建议使用
*/
public void setHeight(float height){
this.height=height;
}
public float getHeight(){
return this.height;
}
}
public class Demo{
public static void main(String args[]){
Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f); //实例化Pupil对象
System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+
"\t成绩:"+p.getScore()+"\t身高:"+p.getHeight()); //打印输出
}
}
private String name;
private int age;
public Person(String name,int age){ //Person的构造方法
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName( ){
return this.name;
}
public int getAge(){
return this.age;
}
}
class Student extends Person{ //Student类,继承Person类
private float score;
public Student(String name,int age,float score){ //Student类构造方法
super(name,age); //调用Person类的构造方法
this.setScore(score);
}
public void setScore(float score){
this.score=score;
}
public float getScore( ){
return this.score;
}
}
class Pupil extends Student{ //Pupil类,继承Student类
private float height; //如果调用Person类的构造方法加上 private float score;
public Pupil(String name,int age,float score,float height){ //Pupil类构造方法
/*调用Student类的构造方法,也可以调用Person类的代码如下:
Super(name,age);*/ super(name,age,score);
this.setHeight(height);
}
/*如果调用Person类的构造方法,则加入score的setter与getter方法:
public void setScore(float score){
this.score=score;
}
public float getScore( ){
return this.score;
}但明显代码重复,不建议使用
*/
public void setHeight(float height){
this.height=height;
}
public float getHeight(){
return this.height;
}
}
public class Demo{
public static void main(String args[]){
Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f); //实例化Pupil对象
System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+
"\t成绩:"+p.getScore()+"\t身高:"+p.getHeight()); //打印输出
}
}
嵌入Java代码看起来好些,改了一个小错误://如果调用Person类的构造方法加上 private float score; 加到private float height; 这一行。 希望对楼主有用。
private String name;
private int age;
public Person(String name,int age){ //Person的构造方法
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName( ){
return this.name;
}
public int getAge(){
return this.age;
}
}
class Student extends Person{ //Student类,继承Person类
private float score;
public Student(String name,int age,float score){ //Student类构造方法
super(name,age); //调用Person类的构造方法
this.setScore(score);
}
public void setScore(float score){
this.score=score;
}
public float getScore( ){
return this.score;
}
}
class Pupil extends Student{ //Pupil类,继承Student类
private float height; //如果调用Person类的构造方法加上 private float score;
public Pupil(String name,int age,float score,float height){ //Pupil类构造方法
/*调用Student类的构造方法,也可以调用Person类的代码如下:
Super(name,age);*/ super(name,age,score);
this.setHeight(height);
}
/*如果调用Person类的构造方法,则加入score的setter与getter方法:
public void setScore(float score){
this.score=score;
}
public float getScore( ){
return this.score;
}但明显代码重复,不建议使用
*/
public void setHeight(float height){
this.height=height;
}
public float getHeight(){
return this.height;
}
}
public class Demo{
public static void main(String args[]){
Pupil p=new Pupil("嘻嘻",10,100.0f,128.0f); //实例化Pupil对象
System.out.println("姓名:"+p.getName()+"\t年龄:"+p.getAge()+
"\t成绩:"+p.getScore()+"\t身高:"+p.getHeight()); //打印输出
}
}
{
public C()
{
System.out.println("C中构造方法!");
}
};
class B extends C
{
public B()
{
System.out.println("B中构造方法!");
}
};
public class A extends B
{
public static void main(String[] args)
{
new A();
}
};
结果是:
C中构造方法!
B中构造方法!