this关键字+内部类

目录

一:this关键字

               1:调用本类属性

               2:调用本类方法

               3:表示当前对象

二:内部类

               外部类访问内部类私有属性

              使用static定义内部类。

              最常用:在方法中定义内部类。 


一:this关键字

1:调用本类属性

class Book{
	private String title;
	private double price;
	public Book(String title,double price) {
		this.title=title;
		this.price=price;
	}
	public String getInfo() {
		return "书名:"+title+";"+"价钱:"+price;
	}
}

public class STATIC {
     public static void main(String[] args) {
       Book A=new Book("JAVA",99);
       System.out.println(A.getInfo());
     } 	
	}

 注意:如果不加this,Java会就近选择变量,也就是函数的参量。如果变量前用this限定,那么便会找类中的属性。
结论:为了避免错误,只要访问类中的属性那么:在属性前加上:this关键字

 2:调用本类方法

1):调用普通方法

class Book{
	private String title;
	private double price;
	public Book(String title,double price) {
		this.title=title;
		this.price=price;
	}
	public void print() {
		System.out.println("/*图书信息:*/");
	}
	public String getInfo() {
		this.print();
		return "书名:"+title+";"+"价钱:"+price;
	}
}

public class STATIC {
     public static void main(String[] args) {
       Book A=new Book("JAVA",99);
       System.out.println(A.getInfo());
     } 	
	}

注意:类内调用类的普通方法,可以直接调用,但为了严谨,最好加上this。

2):调用构造方法(可用于简化重复代码)

之前:

    class Book{
	private String title;
	private double price;
	public Book() {
		
	}
	public Book(String title) {
		this.title=title;
	}
	public Book(String title,double price) {
		this.title=title;
		this.price=price;
	}
	
	public String getInfo() {
		return "书名:"+title+";"+"价钱:"+price;
	}
}

public class STATIC {
     public static void main(String[] args) {
       Book A=new Book();
       System.out.println(A.getInfo());
       Book B=new Book("JAVA");
       System.out.println(B.getInfo());
       Book C=new Book("JAVAc",99);
       System.out.println(C.getInfo());
     } 	
	}

改变后:

class Book{
	private String title;
	private double price;
	public Book() {
		System.out.println("图书信息:");
	}
	public Book(String title) {
		this();//调用无参的构造函数
		this.title=title;
	}
	public Book(String title,double price) {
		this(title);//调用有一个参数的构造函数
		this.price=price;
	}
	
	public String getInfo() {
		return "书名:"+title+";"+"价钱:"+price;
	}
}

public class STATIC {
     public static void main(String[] args) {
       Book A=new Book();
       System.out.println(A.getInfo());
       Book B=new Book("JAVA");
       System.out.println(B.getInfo());
       Book C=new Book("JAVAc",99);
       System.out.println(C.getInfo());
     } 	
	}

注意:this()或this(参数)必须放在第一行。而且不可以递归调用,比如无参构造方法调用了第二个构造方法。所以必须有一个构造方法不要:this。

class Emp{
   private int empno;//编号
   private String ename;//姓名
   private double sal;//工资
   private String dept;//部门
   
   public Emp(int empno,String ename,double sal,String dept) {
	   this.empno=empno;
	   this.ename=ename;
	   this.sal=sal;
	   this.dept=dept;
   }
   public Emp(String ename,double sal) {
	   this(0,ename,sal,"暂无");
   }
   public String getInfo() {
	   return empno+";"+ename+";"+sal+";"+dept;
   }
}

public class STATIC {
     public static void main(String[] args) {
       Emp a=new Emp("张丽",10000);
       System.out.println(a.getInfo());
     } 	
	}

 结果:0;张丽;10000.0;暂无   利用四参构造函数定义二参

3:表示当前对象

 当前对象:当前正在调用类中方法的实例化对象。

 

 对象之间的传递:

class A{
	private B b;
	public A() {//2:执行A类构造
		//3:为B类对象b实例化
		this.b=new B(this);/*4:this表示当前对象,就是tmp*/
		this.b.get();//7:调用B类中的get()方法
	}
	public void print() {
		//10:调用print()输出
		System.out.println("hello,world!");
	}
}
class B{
	private A a;
	//5:把tmp对象传到参数a a=tmp;
	public B(A a) {
		this.a=a;//6:保存a对象,即tmp
	}
	//8:调用此方法
  public void get() {
	  this.a.print();//9:this.a就是传过来的tmp.
  }
  }

public class STATIC {
     public static void main(String[] args) {
      A tmp=new A();//1:实例化对象,调用无参构造方法
     } 	
	}

输出:hello world! 
通过new B(this)把主过程定义的对象赋值给B中对象a,然后对象a再被赋值给A类对象b.
也就是:tmp=B类中的this.a=A类中的this.b;

 总结:this用法
1:类中的属性调用以后都要加上this;
2:类中的构造方法的相互调用。
3:this1表示的当前的对象,不是固定的对象。

二:内部类

所谓内部类就是:在一个类的内部继续定义其他内部结构类的情况

class Outer{//外部类
	private String meg="hello,world!";
	class Inner{//定义一个内部类
		public void print() {
			System.out.println(meg);
		}
		}//内部类终止
	/*实例化内部对象并调用方法print()*/
	public void fun() {
		new Inner().print();
	}
}
public class STATIC {
     public static void main(String[] args) {
    	Outer out=new Outer();
    	out.fun();
     } 	
	}

通过实例化外部类调用外部类的方法,方法内部通过实例化内部类调用内部类的方法。

内部类破坏了类的结构只有属性和方法的结构,但是也会带来方便访问外部类的私有属性的好处。

把Inner类放在外部,同样实现此功能。 
核心:如何让Inner类可以访问到Outer类的私有属性meg.

class Outer{
	private String meg="hello,world!";
	public void fun() {
		new Inner(this).print();
	}
	public String getMeg() {
		return this.meg;
	}
}

class Inner{
	//为了实现一个对象的访问,必须通过应用,所以把主方法的out对象传递给内部类的o.
	private Outer o;
	public Inner(Outer out) {
		this.o=out;
	}
	
	public void print() {
		System.out.println(this.o.getMeg());
	}
	}

public class STATIC {
     public static void main(String[] args) {
    	Outer out=new Outer();
    	out.fun();
     } 	
	}

外部类访问内部类私有属性

class Outer{
	   private String msg="外部类私有属性";
        class Inner{
        	private String info="内部类私有属性";
        	public void print() {
        		System.out.println(msg);
        	}		
        }
        /*实例化内部类对象来访问内部类私有属性。*/
        public void fun() {
        	Inner in=new Inner();
        	System.out.println(in.info);
        }
}

public class STATIC {
     public static void main(String[] args) {
             Outer out=new Outer();
             out.fun();
     } 	
	}

本代码如果:如果内部类访问外部类属性,不能直接加this,而应该外部类.this.属性。即:Outer.this.msg

主程序直接实例化内部类对象。

class Outer{
	   private String msg="外部类私有属性";
        class Inner{
        	private String info="内部类私有属性";
        	public void print() {
        		System.out.println(Outer.this.msg);//msg
        	}		
        }
       
}

public class STATIC {
     public static void main(String[] args) {
            Outer.Inner in=new Outer().new Inner();
            in.print();
     } 	
	}

语法:外部类 内部类  对象名=new 外部类().new 内部类();

使用static定义内部类。

       由于static定义的属性和方法不受类的实例化对象控制,所以内部类被static定义,那么这个类变成了外部类,而且只能访问外部类的static属性和static方法。

class Outer{
	    private static String msg="Outer类属性";
	    static class Inner{
		      public void print() {
			   System.out.println(msg);
		         }
	     }
}

public class STATIC {
     public static void main(String[] args) {
          Outer.Inner in=new Outer.Inner();
          in.print();
     } 	
	}

注意:
1:Inner被static修饰,相当于外部类,所以必须把私有属性变为static才可以被访问。
2:在实例化内部类对象时;外部类。内部类视为一个整体。

最常用:在方法中定义内部类。 

class Outer{
	    private String msg="Outer类属性";
		public void fun() {
			//在fun方法中定义了内部类Inner
		    class Inner{
		    	public void print() {
		    		System.out.println(Outer.this.msg);
		    		  }
		    	  }
		    //fun方法实例化Inner对象调用print()方法
			   new Inner().print();
		         }
	     }


public class STATIC {
     public static void main(String[] args) {
         new Outer().fun();
     } 	
	}
class Outer{
	    private String msg="Outer类属性";
		public void fun(int num) {//num:方法的参数
			double score=100;//方法的属性
		    class Inner{
		    	public void print() {
		    		//内部类从jdk1.8之后可以直接访问:方法的参数和变量
		    		System.out.println(msg+";"+num+";"+score);
		    		  }
		    	  }
			   new Inner().print();
		         }
	     }


public class STATIC {
     public static void main(String[] args) {
         new Outer().fun(100);
     } 	
	}

代码运行结果:Outer类属性;100;100.0

 总结:内部类与外部类可以方便的访问各自的私有属性。内部类被private后不可以在主程序通过外部类.内部类去实例化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值