天天看點

【記錄】C#的通路修飾符以及C#靜态類、靜态方法的使用

目錄

    • C#輸入int類型變量
    • C#通路修飾符及靜态類

記錄

C#輸入int類型變量

無法直接輸入

隻能轉化
using System;

namespace work2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            string str;
            Console.WriteLine("請輸入整數i:");
            str = Console.ReadLine();
            i = int.Parse(str);         //要輸入int型需要轉化
            Console.Write("i*5=",i * 5);
            Console.ReadKey();
        }
    }
}
           
運作結果:
【記錄】C#的通路修飾符以及C#靜态類、靜态方法的使用

C#通路修飾符及靜态類

19.10.14

今日份C#課上知識點

//教室這個輸入法真的是用不慣,回去再寫

string.Empty 隻讀 (不能set)

類庫不會生成.exe

生成.dll 可被調用

調用前需要先引用,然後再用using

項目 即程式集

internal 範圍 本程式集

如果不加的話,預設internal

子類通路父類方法 this. ……();

pravite < protected <internal < public

其中pravite和protected

protected internal 别的程式集的子類

partial Windows窗體應用程式中,用于隔離代碼,自動合并

靜态類無法被執行個體化,是密封的

sealed 密封,無法被繼承

類中可有靜态方法

靜态構造函數,自動執行,僅執行一次

析構函數,先不管

readonly i

隻能在聲明和構造函數中指派

為了和類區分,結構不能繼承
new表達式要求在類型後有()、[]或{}。
控制台的 namespace HL19_10_14TestKnowlege 代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibraryTestK;    //調用類庫前,先引用類庫(添加引用後,再用using)

namespace HL19_10_14TestKnowlege
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            Time.getTime(); //靜态類,隻能直接用
            Test1 t1 = new Test1();
            t1.theCout();
            Test1.cout();   //靜态方法,也是隻能直接調用
            Console.ReadKey();
        }
    }
}

           
類庫的 namespace ClassLibraryTestK 代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//類庫不會生成.exe,生成的是.dll
namespace ClassLibraryTestK
{
    class  Class1   //不寫通路修飾符,預設為internal
    {

    }
    internal class Class2   //範圍:項目(程式集)
    {
    }

    public class Person   //範圍:所有
    {
        public static string getName()
        {
            return string.Empty;    //隻讀
        }
    }

    public class Strdent:Person    //這個就是Person的子類
    {
        string name;
        protected string GetName()  //protected好像不能用于類的前面 範圍:類中和子類中可用
        {
            return name;
        }

        protected internal void SetName(string name)    //範圍:比protected多了跨程式集的子類中也可用
        {
            this.name = name;
        }
    }

    public static class Time   //靜态類是無法被執行個體化的,是密封的,也無法被繼承
    {
        static Time()
        {
            time = "1";
        }
        static string time = "forever";     //聲明成員變量也要加上static
        private static void setTime()   //這個好像也是不能用于類  範圍:類中
        {
            //this.time = "1";  //this在靜态中無效
        }
        public static string getTime() //不能在靜态類中聲明執行個體成員
        {
            Console.WriteLine(time);
            return time;
        }
    }

    public sealed class Time2   //sealed的意思就是不能被繼承
    {

    }

    public class Test1
    {
        public static void cout()   //普通類中的靜态方法
        {
            Console.WriteLine(2);
        }

        public void theCout()
        {
            //this.cout();  //靜态方法不能被執行個體化調用
            cout();
        }
    }
}

           
【記錄】C#的通路修飾符以及C#靜态類、靜态方法的使用
  • https://blog.csdn.net/qq_43763494/article/details/102018253----【BLOG OUTLINE】部落格大綱 ( ̄▽ ̄)"