父类与子类的继承测试方法
代码语言:javascript复制如果父类有值,直接输出父类属性,否则打印输出继承中子类的属性值
代码语言:javascript复制package com.example.core.mydemo.extendtest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CommonConfigure {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private String key;
private String appId;
private String host = "localhost";
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
}
package com.example.core.mydemo.extendtest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CityConfigure extends CommonConfigure {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private String key = "shanghai";
private String appId = "0001";
@Override
public String getKey() {
return key;
}
@Override
public void setKey(String key) {
this.key = key;
}
@Override
public String getAppId() {
return appId;
}
@Override
public void setAppId(String appId) {
this.appId = appId;
}
}
package com.example.core.mydemo.extendtest;
public class MyConfig extends CommonConfigure {
private String key = "mykey";
private String appId = "myAppId";
@Override
public String getKey() {
return key;
}
@Override
public void setKey(String key) {
this.key = key;
}
@Override
public String getAppId() {
return appId;
}
@Override
public void setAppId(String appId) {
this.appId = appId;
}
}
package com.example.core.mydemo.extendtest;
/**
* 父类与子类的继承测试方法
*
* 打印输出: 如果父类有值,直接输出父类属性,否则打印输出继承中子类的属性值
key=shanghai
appId=0001
host=localhost
------------------------------------------
key=mykey
appId=myAppId
host=localhost
*/
public class ExtendTest {
public static void main(String[] args) {
CityConfigure config = new CityConfigure();
print(config);
System.out.println("------------------------------------------");
MyConfig myConfig = new MyConfig();
print(myConfig);
}
public static void print(CommonConfigure config){
System.out.println("key=" config.getKey());
System.out.println("appId=" config.getAppId());
System.out.println("host=" config.getHost());
}
}