我们都知道抽象类不能直接实例化,但是我们还是遇到一些能够实例化的抽象类,那他们到底是如何实例化的呢?根据我查阅到的资料,先总结一下,以后了解会更新。
抽象类
- 1、方法都是静态也有声明为抽象类的
- 2、方法里边有抽象方法
第一种:抽象类是父类,子类实现父类,通过多态实例化
package Abstract_Interface;
abstract class Fu{
public Fu(){
System.out.println("父类构造器");
}
public abstract void show();
}
class Zi extends Fu{
public Zi(){
System.out.println("子类构造器");
}
@Override
public void show(){
System.out.println("子类的show方法");
}
}
public class AbstractClass_Instance {
public static void main(String...s){
Fu fu = new Zi();
fu.show();
}
}
第二种,通过匿名内部类实现实例化(本质上属于第一种)
package Abstract_Interface;
abstract class A{
private String s;
public void setS(String s){
this.s = s;
}
public String getS(){
return this.s;
}
public abstract void show();
public static A newInstance(){
return new A(){public void show(){
System.out.println("匿名内部类实际就是一个A的子类");
}};
}
}
public class AbstractClass_Instance2 {
public static void main(String[]args){
A a = A.newInstance();
a.show();
}
}
DateFormat的实例化探究
DateFormat是抽象类,继承体系
我们通过它提供的静态工厂方法可以实例化:
* static DateFormat getDateInstance() 获取具有默认 FORMAT语言环境的默认格式样式的日期格式化程序。
* static DateFormat getDateInstance(int style) 获取具有默认 FORMAT语言环境的给定格式样式的日期格式化程序。
* static DateFormat getDateInstance(int style, Locale aLocale) 获取具有给定语言环境的给定格式样式的日期格式化程序。
* static DateFormat getDateTimeInstance() 获取具有默认 FORMAT语言环境的默认格式样式的日期/时间格式化程序。
* static DateFormat getDateTimeInstance(int dateStyle, int timeStyle) 获取具有默认 FORMAT语言环境的给定日期和时间格式样式的日期/时间格式化程序。
* static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 获取具有给定语言环境的给定格式样式的日期/时间格式化程序。
* static DateFormat getInstance() 获取一个默认的日期/时间格式化程序,它使用SHORT样式作为日期和时间。
猜想是通过SimpleDateFormat多态实例化——看源码。本着探究精神!看了一下源码
1、第一次看到了get函数
2、查看get()函数
private static DateFormat get(int timeStyle, int dateStyle,
int flags, Locale loc) {
if ((flags & 1) != 0) {
if (timeStyle < 0 || timeStyle > 3) {
throw new IllegalArgumentException("Illegal time style " + timeStyle);
}
} else {
timeStyle = -1;
}
if ((flags & 2) != 0) {
if (dateStyle < 0 || dateStyle > 3) {
throw new IllegalArgumentException("Illegal date style " + dateStyle);
}
} else {
dateStyle = -1;
}
LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, loc);
DateFormat dateFormat = get(adapter, timeStyle, dateStyle, loc);
if (dateFormat == null) {
dateFormat = get(LocaleProviderAdapter.forJRE(), timeStyle, dateStyle, loc);
}
return dateFormat;
}
DateFormat dateFormat = get(adapter, timeStyle, dateStyle, loc);
3、get里边又是get()
4、此时探求到了一个叫DateFormatProvider的类,使用了其getInstance(),查阅该类的getInstance()方法
private DateFormat getInstance(int dateStyle, int timeStyle, Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
// Check for region override
Locale rg = CalendarDataUtility.findRegionOverride(locale);
SimpleDateFormat sdf = new SimpleDateFormat("", rg);
Calendar cal = sdf.getCalendar();
try {
String pattern = LocaleProviderAdapter.forType(type)
.getLocaleResources(rg).getDateTimePattern(timeStyle, dateStyle,
cal);
sdf.applyPattern(pattern);
} catch (MissingResourceException mre) {
// Specify the fallback pattern
sdf.applyPattern("M/d/yy h:mm a");
}
// Check for timezone override
String tz = locale.getUnicodeLocaleType("tz");
if (tz != null) {
sdf.setTimeZone(
TimeZoneNameUtility.convertLDMLShortID(tz)
.map(TimeZone::getTimeZone)
.orElseGet(sdf::getTimeZone));
}
return sdf;
}
出现了!!
SimpleDateFormat sdf = new SimpleDateFormat("", rg);
.......
return sdf;
所以它也是通过第一种方法来实现的!父类指向子类!