zoukankan      html  css  js  c++  java
  • Java-C#-内部类

    Java

    code:

    public class Outer{
        private int outerNum = 1;
        
        public int getOuterNum() {
    		return outerNum;
    	}
        
        public class Inner{
        	//错误,非静态内部类不能有静态成员
        	//static int num;
    
            public void func(){
                //可以直接访问外部类的成员
                outerNum++;
            }
        }
        
        public void func(){
        	var inner=new Inner();
        	inner.func();
        }
    }
    
    public class Program {
    	
    	public static void main(String[] args) {
    		
    		var outer=new Outer();
    		System.out.println(outer.getOuterNum());
    		
    		outer.func();
    		System.out.println(outer.getOuterNum());
    		
    		var inner=outer.new Inner();
    		inner.func();
    		System.out.println(outer.getOuterNum());
    	}
    	
    }
    
    

    result:

    C#

    code:

    public class Outer
    {
        private int outerNum=1;
    
        public int OuterNum { get => outerNum;}
    
        public class Inner
        {
            static int num;
    
            public void Func()
            {
                //错误,无法访问外部类的非静态成员
                //outerNum++;
    
                //通过外部类的实例来访问非静态成员
                var outer = new Outer();
                outer.outerNum++;
            }
        }
    
        public void Func()
        {
            var inner = new Inner();
            inner.Func();
        }
    }
    
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var outer = new Outer();
            Console.WriteLine(outer.OuterNum);
    
            outer.Func();
            Console.WriteLine(outer.OuterNum);
    
            var inner = new Outer.Inner();
            inner.Func();
            Console.WriteLine(outer.OuterNum);
    
            await Task.CompletedTask;
        }
    }
    

    result:

  • 相关阅读:
    iBatis,第二次亲密接触
    微斯人,吾谁与归
    一个月了
    生命在于运动
    眼皮跳了好几天
    往返
    中病毒,学习批处理

    爱如潮水
    今天夏至
  • 原文地址:https://www.cnblogs.com/LaughAtSelfsWrong/p/13890618.html
Copyright © 2011-2022 走看看