java中子类会继承父类的构造方法吗
展开全部
是的,继承是子类会继承父类的属性和方法,构造方法也是属于方法,所以会继承构造方法,例如:
public class concept {
public static void main(String[] args){
Chicken c=new Chicken("小白");
c.desc();
System.out.println("**********************");
HomeChicken hc=new HomeChicken();
hc.desc();
System.out.println("**********************");
//创建对象其实就是调用构造方法
Pheasant p=new Pheasant("小花");
p.desc();
}
}
//父类(超类)
class Chicken{
String name;
//若父类没有默认方法,则加入参数
public Chicken(String name){
System.out.println("Chicken"+name);
}
public void desc(){
System.out.println("我是一只鸡");
}
}
//子类
class HomeChicken extends Chicken{
public HomeChicken(){
super("小白");
System.out.println("HomeChicken");
}
}
//子类野鸡类
class Pheasant extends Chicken{
public Pheasant(String name){
super(name);
this.name =name;
System.out.println("Pheasant");
}
}
在这里,
public class concept {
public static void main(String[] args){
Chicken c=new Chicken("小白");
c.desc();
System.out.println("**********************");
HomeChicken hc=new HomeChicken();
hc.desc();
System.out.println("**********************");
//创建对象其实就是调用构造方法
Pheasant p=new Pheasant("小花");
p.desc();
}
}
//父类(超类)
class Chicken{
String name;
//若父类没有默认方法,则加入参数
public Chicken(String name){
System.out.println("Chicken"+name);
}
public void desc(){
System.out.println("我是一只鸡");
}
}
//子类
class HomeChicken extends Chicken{
public HomeChicken(){
super("小白");
System.out.println("HomeChicken");
}
}
//子类野鸡类
class Pheasant extends Chicken{
public Pheasant(String name){
super(name);
this.name =name;
System.out.println("Pheasant");
}
}
在这里HomeChicken 继承父类Chicken,在子类HomeChicken中,也继承了父类的Chicken()方法
public class concept {
public static void main(String[] args){
Chicken c=new Chicken("小白");
c.desc();
System.out.println("**********************");
HomeChicken hc=new HomeChicken();
hc.desc();
System.out.println("**********************");
//创建对象其实就是调用构造方法
Pheasant p=new Pheasant("小花");
p.desc();
}
}
//父类(超类)
class Chicken{
String name;
//若父类没有默认方法,则加入参数
public Chicken(String name){
System.out.println("Chicken"+name);
}
public void desc(){
System.out.println("我是一只鸡");
}
}
//子类
class HomeChicken extends Chicken{
public HomeChicken(){
super("小白");
System.out.println("HomeChicken");
}
}
//子类野鸡类
class Pheasant extends Chicken{
public Pheasant(String name){
super(name);
this.name =name;
System.out.println("Pheasant");
}
}
在这里,
public class concept {
public static void main(String[] args){
Chicken c=new Chicken("小白");
c.desc();
System.out.println("**********************");
HomeChicken hc=new HomeChicken();
hc.desc();
System.out.println("**********************");
//创建对象其实就是调用构造方法
Pheasant p=new Pheasant("小花");
p.desc();
}
}
//父类(超类)
class Chicken{
String name;
//若父类没有默认方法,则加入参数
public Chicken(String name){
System.out.println("Chicken"+name);
}
public void desc(){
System.out.println("我是一只鸡");
}
}
//子类
class HomeChicken extends Chicken{
public HomeChicken(){
super("小白");
System.out.println("HomeChicken");
}
}
//子类野鸡类
class Pheasant extends Chicken{
public Pheasant(String name){
super(name);
this.name =name;
System.out.println("Pheasant");
}
}
在这里HomeChicken 继承父类Chicken,在子类HomeChicken中,也继承了父类的Chicken()方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |