在java中,当创建一个子类对象会不会先创建父类对象?(如果父类是抽象类呢?)

https://blog.csdn.net/banzhengyu/article/details/81039757

在java中,当创建一个子类对象会不会先创建父类对象?(如果父类是抽象类呢?)

提个问题:如果当创建一个子类对象会先创建父类对象的话,那么如果父类是抽象类的话,那该抽象父类也会被实例化喽,这与抽象类不能实例化相互矛盾啊!

package javase5;

public abstract class Animal {
	int age;
	
	public Animal() {
		this.age = 6;
		System.out.println("我是Animal类的无参构造函数");
	}
}
package javase5;

public class Cat extends Animal{
	
	public Cat() {
//		super(); 
//就算不写super();这行代码,系统也会默认自动调用父类的无参构造函数
		System.out.println("我是Cat类的无参构造函数");
	}

	public static void main(String[] args) {
		Cat cat = new Cat();
		System.out.println(cat.age);
		
		Cat cat2 = new Cat();
		System.out.println(cat2.age);
	}

}

 

发布了637 篇原创文章 · 获赞 638 · 访问量 140万+
展开阅读全文

spring 依赖注入的困惑 关于子类

12-10

有一个Son类 package cn.extend; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; public class Son extends Father { } 还有一个父类Father package cn.extend; public class Father { private String name; protected int age; public Long maney; private GrandFather grandFather;//这里是private私有成员 public GrandFather getGrandFather() { return grandFather; } public final void setGrandFather(GrandFather grandFather) { //这里声明final 子类不可以重写这个方法 this.grandFather = grandFather; System.out.println("father final set Method---->"+this.grandFather.showFather()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Long getManey() { return maney; } public void setManey(Long maney) { this.maney = maney; } } 一个爷爷类GrandFather package cn.extend; public class GrandFather { public String showFather() { return "i am your father"; } } 那么我们在spring的applicationContext.xml配置文件中 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="grandFather" class="cn.extend.GrandFather"></bean> <bean id="father" class="cn.extend.Father"></bean> <bean id="son" class="cn.extend.Son" > <property name="name" value="kenan"></property> <property name="age" value="22"></property> <property name="maney" value="100"></property> <property name="grandFather"> <ref bean="grandFather"></ref> </property> </bean> </beans> 问题来了 我在Son的<bean 配置里面写上了父类所有的成员属性 而且我的setGrandFather()方法还是final的 测试类 public class TestExtends { @Test public void testExtend(){ ApplicationContext context=new ClassPathXmlApplicationContext("cn/extend/applicationContext.xml"); Father father=(Father)context.getBean("father"); Son son=(Son)context.getBean("son"); System.out.println(father.getAge()+father.getName()); System.out.println("father--->"+father.getGrandFather()); System.out.println(son.getAge()+son.getName()); System.out.println("son--->"+son.getGrandFather().showFather()); } } 输出的结果 father final set Method---->i am your father(这句话说明父类的这个属性已经被赋值了) 0null(父类的属性也都是为空,但是神奇的是子类可以获得到) father--->null(问题的是,为什么从容器中取出来的father调用getGrandFather()方法就是为null的呢) 22kenan son--->i am your father(而且子类是可以获得父类的GrandParent()还可以拿出来,但是father类就是null, 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览