内部类与外部类 使用 及 instanceof
来源:互联网 发布:猫头鹰安全座椅 知乎 编辑:程序博客网 时间:2024/06/07 01:57
class A{
void printA(){
}
}
class B extends A{
void printB(){
}
}
class C extends A{
void printC(){
}
}
public class TestStringBuilder {
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
//begin------StringBuilder use---------
String str = "";
long startTime =System.currentTimeMillis();
for (inti=0;i<10000;i++){
str+=i;
}
long endTime =System.currentTimeMillis();
long Time = endTime -startTime;
System.out.println("totaltime is锛?" + Time);
StringBuilder builder = newStringBuilder("");
startTime =System.currentTimeMillis();
for (inti=0;i<10000;i++){
builder.append(i);
}
endTime =System.currentTimeMillis();
Time = endTime - startTime;
System.out.println("stringbuildertotal time: " + Time);
//end------StringBuilder end---------
//---------sort arraybegin----------
int[] testNum ={10,40,1,50,88,100};
for (inti=0;i<testNum.length;i++){
int maxIndex = 0;
int max = testNum[maxIndex];
for (intj=1;j<testNum.length-i;j++){
if (max >testNum[j]){
testNum[maxIndex] =testNum[j];
testNum[j]= max;
maxIndex = j;
}else{
maxIndex = j;
max =testNum[j];
}
}
}
for (int i=0;i<testNum.length;i++){
System.out.println(testNum[i]);
}
//---------sort arrayend----------
//---------begin instanceof usedfor downcast -------
//A a = new B(); // a is instaceofB
A a = new A(); //a is not instanceof B
B b = new B();
if(a instanceof B){
b =(B)a;
System.out.println("a isinstance of B");
}
//---------end instanceof used fordowncast -------
//--begin final array values canbe changed
final int[] arr = {10,42,1};
int[] arr_2 = {2,7,9};
final int[] f_arr = arr;
for (int i =0;i<arr.length;i++){
f_arr[i] = i + 1;
arr[i] = i + 2;
System.out.println(":"+ arr[i] + ":" + f_arr[i]);
}
//--end final array values can bechanged
//--begin ----------inner and outer class----
Outer out = new Outer();
Outer.Inner inner = out.newInner();
Outer.Inner in = out.fun();
//--end ----------inner and outerclass----
//begin inner class
OuerInterface bb = newOuterClass().doInterface();
OuerInterface aa = newMyClass().doInterface();
aa.fun();
bb.fun();
//end inner class
}
}
//1:inner class instance associated to outer class instance
//2:inner class can access outer class's private funs and vars
//3:outer class's function can instant inner class as a var and can be accessby other inner class
//4:outer class can not access inner class's var,but can access by a instanceof inner class in case 3:
//5:inner class can only be instant by instance of outer class
class Outer {
private int outterVar = 1;
Inner in = new Inner();
Inner fun(){
in.innerVar = 4;//can access
//innerVar = 5; //can notaccess
return new Inner();
}
class Inner {
int innerVar = 1;
}
}
//1:inner class can be private or protected but outer class can not
//2:inner class implements interface but as a private class, so can not bedowncast as InnerClass
//3:only a interface and a outer class can be access so it very well for hidefor fun implements
interface OuerInterface{
void fun();
}
class OuterClass {
private class InnerClass implementsOuerInterface {
public void fun(){
}
}
public OuerInterfacedoInterface(){
return new InnerClass();
}
}
class MyClass extends OuterClass{
}
内部类与外部类 使用 instanceof
内部类实例依附于外部类的实例,内部类可以访问外部类的private 变量与方法,外部类只能通过在外部类中实例(new)一个内部类或者在非静态方法中实例一个内部类返回来访问内部类域与方法
Outclass outer =new outclass
Inner inner =outer.new inner();
Inner inner =outer.fun();
- 内部类与外部类 使用 及 instanceof
- 内部类与外部类
- 内部类与外部类
- 内部类与外部类
- 内部类与外部类
- 【Java】内部类与外部类的互访使用小结
- 【Java】内部类与外部类的互访使用小结
- Scala 内部类及外部类
- Java内部类与外部类
- 内部类 外部类区别与联系
- java 内部类与外部类学习
- 内部类 外部类区别与联系
- 内部类 外部类区别与联系
- 外部类与内部类的调用
- 内部类 外部类
- 内部类、外部类
- 内部类,外部类成员的相互访问 及内部类对象的创建
- 内部类使用场景:供外部类使用,回调
- Android Studio恢复移除的Module
- 堆排序
- github 建立博客
- Android 添加系统关机重启项
- 能控性与极限性能指标
- 内部类与外部类 使用 及 instanceof
- iOS开发中模拟器归档成功,但是真机归档失败的问题
- Android多媒体(1)——基础知识与音频
- Android Settings 修改默认主题为白底黑字
- 信息传递(tarjan算法)
- Github起手式
- GPS NEMA 0183协议
- php过滤字符串
- .NET平台 C# OpenGL程序