2013-03-07 99 views
0

我對類和方法有基本的瞭解。我可以做類,並定義方法他們:一個類中的C#名稱空間

myClass.awesome("test"); (example) 

但是我看到一個類有以下方法:

anotherClass.something.methodName(arguments); 

如何創建具有額外的命名空間(S)的方法。我想:

public Class test 
{ 
    namespace subname 
    { 
     public void Test() 
     { 
      return; 
     } 
    } 

    //also i tried: 
    public void lol.Test() 
    { 
     return; 
    } 
} 

但他們都表示,它不這樣做,怎麼做是正確的,所以我可以在我的方法更好地訂購/組?

請不要問爲什麼,或者給一個選擇,我只是想有這樣的方法(Class.sub.Method()Class.sub.sub....sub.Method()

感謝您閱讀我的問題,並可能給ANS答案類:)

+1

做什麼correctley?你不能在類中聲明命名空間。 – Jodrell 2013-03-07 10:50:13

+0

好的,下面是你的答案:看看[這個鏈接](http://www.codeproject.com/Articles/22769/Introduction-to-Object-Oriented-Programming-Concep)其關於[Object Oriented Programming] (http://en.wikipedia.org/wiki/Object-oriented_programming) – 2013-03-07 10:54:30

回答

6

我看到一個類有以下方法: anotherClass.something.methodName(arguments);

該方法不是從類anotherClass而是來自對象something的類。

anotherClass有一個字段/屬性something這是另一個類類型,該類具有方法methodName

0

你認爲你所看到的是不正確的實際。它可以是任何如下的:

  • anotherClass是命名空間,something是類,methodName是靜態方法
  • anotherClass是一個對象,somethinganotherClass的屬性,methodName是一種方法
0

如果你想分組你的方法,你應該考慮使用這樣的靜態類:

public class Test 
{ 
    public static class InnerGroup 
    { 
     public static void Method1() { } 
    } 
    public static class AnotherInnerGroup 
    { 
     public static void Method2() { } 
    } 
} 

或類屬性是這樣的:

public class Test 
{ 
    public class InnerGroup 
    { 
     public static void Method1() { } 
    } 

    public class AnotherInnerGroup 
    { 
     public static void Method2() { } 
    } 

    public InnerGroup InnerGroup { get; set; } 

    public AnotherInnerGroup AnotherInnerGroup { get; set; } 

    public Test() 
    { 
     InnerGroup = new InnerGroup(); 
     AnotherInnerGroup= new AnotherInnerGroup(); 
    } 
} 

希望你明白。

相關問題