父類和子類物件的獲取值的方式驗證,透過父類屬性的方式獲取不到值,需要使用get方法
靜態屬性透過類.屬性的方式獲取,物件獲取使用get方法獲取
package com.example.core.mydemo.java; public class BasePartner { private String partnerName = ""; private String channelName = ""; public String getPartnerName() { return partnerName; } public void setPartnerName(String partnerName) { this.partnerName = partnerName; } public String getChannelName() { return channelName; } public void setChannelName(String channelName) { this.channelName = channelName; } /** * partnerName: //透過父類屬性的方式獲取不到值,需要使用get方法 * channelName: //透過父類屬性的方式獲取不到值,需要使用get方法 * partnerName2:合作商名稱 * channelName2:渠道商名稱 * * @param args */ public static void main(String[] args) { BasePartner partner = new SubPartner(); System.out.println("partnerName:" + partner.partnerName); System.out.println("channelName:" + partner.channelName); System.out.println("partnerName2:" + partner.getPartnerName()); System.out.println("channelName2:" + partner.getChannelName()); } } package com.example.core.mydemo.java; public class SubPartner extends BasePartner{ private String partnerName = "合作商名稱"; private String channelName = "渠道商名稱"; public static int MAX = 100; @Override public String getPartnerName() { return partnerName; } @Override public void setPartnerName(String partnerName) { this.partnerName = partnerName; } @Override public String getChannelName() { return channelName; } @Override public void setChannelName(String channelName) { this.channelName = channelName; } /** * partnerName3:合作商名稱 //物件自身的屬性值可以獲取 * channelName3:渠道商名稱 //物件自身的屬性值可以獲取 * partnerName4:合作商名稱 * channelName4:渠道商名稱 * MAX=100 靜態屬性透過類.屬性的方式獲取,物件獲取使用get方法獲取 * @param args */ public static void main(String[] args) { SubPartner subpartner = new SubPartner(); System.out.println("partnerName3:" + subpartner.partnerName); System.out.println("channelName3:" + subpartner.channelName); System.out.println("partnerName4:" + subpartner.getPartnerName()); System.out.println("channelName4:" + subpartner.getChannelName()); System.out.println("MAX=" + SubPartner.MAX); } }