黑马程序员――面向对象--内部类
发布时间:2020-12-13 20:23:00 所属栏目:PHP教程 来源:网络整理
导读:------Java培训、Android培训、iOS培训、.Net培训、期待与您交换! ------- 内部类: 内部类的访问规则: 1.内部类可以直接访问外部类中的成员,包括私有 之所以可以直接访问外部类中的成员,是由于内部类中持有了1个外部类的援用,式: 外部类名.this 2.外
------Java培训、Android培训、iOS培训、.Net培训、期待与您交换! ------- 内部类: 内部类的访问规则: class outer{
int num = 5;
class inner{
System.out.println("num="+num);//可以直接调用外部成员
}
void method(){
inner in = new inner();//创建内部类的对象
show();//调用内部类的方法
}
} 访问格式:1.当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中 可以直接建立内部类对象 格式: 外部类名.内部类名 变量名 = 外部类对象.内部类对象; outer.Inner in = new outer().new Inner(); 2.当内部类在成员位置上,就能够被成员修饰符所修饰 比如:private:将内部类在外部类中进行封装 static:内部类就具有static的特性 当内部类被static修饰后,只能直接访问外部类中的static成员,出现了访问局限 在外部其他类中,如何直接访问static内部类的非静态成员呢? new outer.Inner().function(); 在外部其他类中,如何直接访问static内部类的静态成员呢? outer.Inner.function(); 注意:当内部类中定义了静态成员,该内部类必须是static的 当外部类中的静态方法访问内部类时,内部类也必须是static的 当描写事物时,事物的内部还有事物,该事物类用内部类来描写。 由于内部事务在使用外部事物的内容 class Body{
private class zhangcheng//内部类
{
}
public void show()
{
new zhangcheng();//必须建立对象
}
}
class Outer
{
private static int x = 3;
static class Inner
{
static void function(){
System.out.println("inner:"+x);
}
}
static class Inner2{
void show(){
System.out.println("inner2 show");
}
}
public static void method(){
new Inner2().show();
}
}
class InnerClassDemo2
{
public static void main(String args[]){
Outer.Inner.function();
new Outer.inner().function();//直接访问内部类中的成员
Outer.Inner in = new Outer().new Inner();//创建内部类对象
in.function();//调用内部类的方法
}
} 内部类在定义时:1.不可以被成员修饰符修饰 2.可以直接访问外部类中的成员,由于还持有外部类中的援用 但是不可以访问他所在的局部中的变量,只能访问被final修饰的局部变量 class Outer
{
int x = 3;
void method(final int a){
final int y = 4;
class Inner{
void function(){
System.out.println(y);
}
}
new Inner().function();
}
class InnerClassDemo{
public static void main(String args[]){
Outer out = new Outer();
out.method(7);
out.method(8);
}
} 匿名内部类:1.匿名内部类其实就是内部类的简写格式 2.匿名内部类的条件: 内部类必须是继承1个类或是实现接口 3.匿名内部类的格式: new 父类或接口(){定义子类的内容} 4.其实匿名内部类就是1个匿名子类对象,而且这个对象有点胖,可以理解为带内容的对象 5.匿名内部类中定义的方法最好不要超过3个。 abstract class AbsDemo{
abstract void show();
}
class outer{
int x = 3;
class Inner extends AbsDemo{
int num = 90;
void show(){
System.out.println('show:"+num);
}
void abc(){
System.out.println("abc");
}
}
public void function(){
AbsDemo d = new AbsDemo(){
int num = 9;
void show(){
System.out.println("num="+num);
}
void abc(){
System.out.println("abcccc");
}
};//注意这个分号
d.show();//ok,实现了接口
d.abc();//error,自定义方法,没有实现接口或是继承1个类,抽象类不能继承,只能实现
}
}
class InnerclassDemo{
public static void main(String args[]){
new Outer().function();
}
}
interface Inter{
void method();
}
class test{
static class Inner implements Inter
{
public void method()
{
System.out.println("method run");
}
}
static Inter function(){
return new Inter(){
public void method(){
System.out.println("method run");
}
}; }
}
class Innerclasstest
{
public static void main(String args[]){
//test.function():test类中有1个静态方法function
//method():function这个方法运算后的解惑是1个对象,而且是1个Inner类型的对象
//由于只有是Inter类型的对象,才可以调用method方法
test.function().method();
inter in = test.function();
in.method();
show(new Inter()
{
public void method()
{
System.out.println("method show run");
}
});
}
public static void show(Inter in)
{
in.method();
}
}
class Innertest
{
public static void main(String args[]){
new object()
{
public static function()
{
}
}.function();
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |