Partial
Partial Class | Partial Method |
---|---|
存取修飾詞要一致 | 不能加任何存取修飾詞,因其本身預設是 Private |
回傳型態僅能為string 、int 、void ...等 |
回傳型態僅能為void |
若要使用 Partial Method Step 1:得先宣告 Step 2:再實作 ( 宣告 與 實作 可以在同名的 Partial Class 分開做沒關係 ) 若要呼叫 Partial Method,得必須透過所屬的 Class 裡面的 Method 呼叫使用 不能被所屬的Class物件成員直接使用 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PartialDemo
{
class Program
{
static void Main(string[] args)
{
SubClass A = new SubClass();
A.Name = "王小明";
A.GameScore = 100;
//呼叫 Interface 的 Method
A.face_fn1();
//呼叫 Partial Method
A.callpartialfn();
}
}
public class FatherClass
{
}
public interface face1
{
void face_fn1();
}
/*
* 相同名稱的 Partial Class 一定要有相同的存取修飾詞,否則會有錯誤
*/
partial class SubClass : FatherClass
{
#region Field
private string _name;
private int _gameScore;
#endregion
#region Property
public string Name
{
get { return _name; }
set { _name = value; }
}
#endregion
#region Partial Method
/*
* Partial Method 不能加任何存取修飾詞,因其本身預設是 Private
* Partial Method 的回傳型態僅能為「void」
*
* 若要使用 Partial Method
* Step 1:得先宣告
* Step 2:再實作 ( 宣告 與 實作 可以在同名的 Partial Class 分開做沒關係 )
*
* 若要呼叫 Partial Method,得必須透過所屬的 Class 裡面的 Method 所使用
* 不能被所屬的Class物件成員直接使用
*/
//Step 1:宣告Partial Method
partial void subclass_partialfn1();
//Step 2:實作Partial Method
partial void subclass_partialfn1()
{
Console.WriteLine("這是subclass_partialfn1()");
}
public void callpartialfn()
{
subclass_partialfn1();
}
#endregion
}
partial class SubClass : face1
{
#region Property
public int GameScore
{
get { return _gameScore; }
set { _gameScore = value; }
}
#endregion
#region Method
public void face_fn1()
{
Console.WriteLine("實作Interface的方法。");
}
#endregion
}
}