马牛的C#学习(第五天)-定义类成员

using System;
using System.Collections.Generic;
using System.Text;
//定义类成员
//定义字段
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            myClass myobj = new myClass();
            // myobj.myInt = 3;     静态成员只能通过类名来访问,而不能通过对象的实例来访问    Error 1 Static member 'ConsoleApplication1.myClass.myInt' cannot be accessed with an instance reference; qualify it with a type name instead
            myClass.myInt = 3;     //这样(通过定义的类)就可以访问了
            // myobj.myReadOnlyInt = 4;   readonly类型的成员只能在执行构造函数的过程中赋值,或由初始化赋值语句赋值   Error 1 A readonly field cannot be assigned to (except in a constructor or a variable initializer) 
        }
    }

    class myClass
    {
        public static int myInt;
        public readonly int myReadOnlyInt;

        public myClass()    //执行构造函数的过程中赋值
        {
            myReadOnlyInt = 4;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Text;
//定义方法的一般过程
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myobj = new MyClass();
            Console.WriteLine("{0}",myobj.GetString());
        }
    }

    class MyClass
    {
        public string GetString()
        {
            return "hear is a string";
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Text;
//方法重载的例子
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDerivedClass myobj = new MyDerivedClass();
            myobj.DoSomething();
            MyDerivedClass2 myobj2 = new MyDerivedClass2();
            myobj2.DoSomething ();
        }
    }

    public class MyBaseClass
    {
        public virtual void DoSomething()
        {
            Console.WriteLine("this is MyBaseClass.DoSomething()");
        }
    }

    public class MyDerivedClass : MyBaseClass
    {
        public override void DoSomething()  //重写方法
        {
            Console.WriteLine("this is MyDerivedClass.DoSomething()");
        }
    }

    public class MyDerivedClass2 : MyBaseClass
    {

    }
}

运行结果:

using System;
using System.Collections.Generic;
using System.Text;
//p194
//使用字段方法和属性
namespace ConsoleApplication1
{
    class MyClass
    {
        public readonly string Name;
        public int intVal;

        public int Val
        {
            get
            {
                return intVal;
            }
            set
            {
                if (value >= 0 && value <= 10)
                    intVal = value;
                else
                    throw (new ArgumentOutOfRangeException("val", value, "Val must be assign a value between 0 and 10."));
            }
        }

        public override string ToString()
        {
            return "Name:" + Name + "/nVal:" + Val;
        }

        private MyClass():this("Default Name")
        {
        }

        public MyClass(string newName)
        {
            Name = newName;
            intVal = 0;
        }


        static void Main(string[] args)
        {
            Console.WriteLine("Creating object myObj...");
            MyClass myObj = new MyClass("My Object");
            Console.WriteLine("myObj created");
            for (int i = -1; i <= 0; i++)
            {
                try
                {
                    Console.WriteLine("/nAttempting to assign {0} to myObj.Val...", i);
                    myObj.Val = i;
                    Console.WriteLine("value{0} assigned to myObj.val.", myObj.Val);
                }
                catch (Exception e)   // i不在0~10之间时出错,这个错误传给e
                {
                    Console.WriteLine("exception {0} thrown.", e.GetType().FullName);
                    Console.WriteLine("Message:/n/"{0}/"", e.Message);

                }
            }
            Console.WriteLine("/nOutputting myObj.ToString()...");
            Console.WriteLine(myObj.ToString());
            Console.WriteLine("myobj.tostring() output.");

        }
    }
}

结果:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值