亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從其他類方法訪問類方法

從其他類方法訪問類方法

C#
慕勒3428872 2021-11-14 17:08:54
我有一類字體,如:namespace Project.Utility{  public class Fonts    {        public Font TitleFont()        {            var font = new Font("Arial", 12, FontStyle.Bold);            return font;        }        public Font SubtitleFont()        {            var font = new Font("Arial", 8, FontStyle.Italic);            return font;        }        public Font TotalMinutesFont()        {            var font = new Font("Arial", 8, FontStyle.Regular);            return font;        }    }}所以進入另一個類,我想像這樣調用字體方法: using Project.Utility; private void MyMethod(){                  title.Font = Fonts.TitleFont;                        }但我無權訪問.TitleFont如何調用它以訪問此方法?問候
查看完整描述

2 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

添加我在評論中所說的內容以及 Piotr 在他的回答中開始的內容。我建議將Fonts類更改為靜態(因為它的所有方法都不依賴于狀態)。


public static class Fonts

{

    public static Font TitleFont()

    {

        var font = new Font("Arial", 12, FontStyle.Bold);

        return font;

    }


    public static Font SubtitleFont()

    {

        var font = new Font("Arial", 8, FontStyle.Italic);

        return font;

    }


    public static Font TotalMinutesFont()

    {

        var font = new Font("Arial", 8, FontStyle.Regular);

        return font;

    }

}

然后,您可以Fonts使用您在示例中提供的語法來訪問這些方法。


查看完整回答
反對 回復 2021-11-14
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

使方法TitleFont靜態


public static Font TitleFont()

{

    return new Font("Arial", 12, FontStyle.Bold);

}

然后做:


using Project.Utility;


private void MyMethod(){

              title.Font = Fonts.TitleFont();

                    }


查看完整回答
反對 回復 2021-11-14
  • 2 回答
  • 0 關注
  • 218 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號