C#语言基础(四)

  
方法
25、方法的参数
using System;
using System.Collections.Generic;
using System.Text;
// 方法的参数,代码声明一个返回值为string类型的静态方法country,并接收一个string类型参数。
namespace MethodTestcountry
{
    class TestCountry
    {
        public static string country(string strCountry)
        {
            return " 我是" + strCountry;
        }
        static void Main()
        {
            Console.WriteLine(country(" 中国人" ));
        }
    }
}
26、params参数
using System;
using System.Collections.Generic;
using System.Text;
//params 参数是指在方法的参数数目可变处采用的参数。params参数必须是一维数组。
// 代码声明一个静态方法UseParams,并接收一个string[]类型参数,利用循环输出数组的元素。
namespace MethodTestParams
{
    class TestParams
    {
        public static void UseParams(params string[] list)
        {
            for (int i = 0; i < list.Length; i++)
            {
                Console.WriteLine(list[i]);
            }
        }
        static void Main()
        {
            string[] strName = new string[5] { " 我" , " 是" , " 中" , " 国" , " 人" };
            UseParams(strName);
        }
    }
}
27、ref参数
using System;
using System.Collections.Generic;
using System.Text;
//ref 参数使参数按引用传递。其效果是当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
// 若要使用ref参数,则方法定义和调用方法都必须显式使用ref关键字。
namespace MethodTestRef
{
    class TestRef
    {
        public static void Method(ref int i)
        {
            i = 44;
        }
        public static void Main()
        {
            int val = 0;
            Method(ref val);
            Console.WriteLine(val);
        }
    }
}
28、out参数
using System;
using System.Collections.Generic;
using System.Text;
//out 关键字会导致参数通过引用来传递。这与ref关键字类似,不同之处在于ref要求变量必须在传递之前进行初始化。
// 若要使用out参数,方法定义和调用方法都必须显式使用out关键字。
namespace MethodTestOut
{
    class TestOut
    {
        public static void Method(out int i)
        {
            i = 44;
        }
        public static void Main()
        {
            int value;
            Method(out value);
            Console.WriteLine(value);
        }
    }
}
29、静态方法
using System;
using System.Collections.Generic;
using System.Text;
// 静态方法不对特定实例进行操作,在静态方法中引用this会导致编译错误。
class Class1
{
    public static string Country()
    {
        string strCountry = " 中国" ;
        return strCountry;
    }
}
class StaticMethod
{
    static void Main(string[] args)
    {
        Console.WriteLine(Class1.Country().ToString());
    }
}
30、非静态方法
using System;
using System.Collections.Generic;
using System.Text;
// 非静态方法是对类的某个给定的实例进行操作,而且可以用this来访问该实例。
class Class1
{
    public string Country()
    {
        string strCountry = " 实例方法的示例!" ;
        return strCountry;
    }
    public string stradd()
    {
        Class2 class2 = new Class2();
        string strField = class2.Cc() + this.Country();
        return strField;
    }
}
class Class2
{
    public string Cc()
    {
        string strCc = " 这是一个" ;
        return strCc;
    }
    static void Main(string[] args)
    {
        Class1 class1 = new Class1();
        Console.WriteLine(class1.stradd());
    }
}
31 、方法的重载
using System;
using System.Collections.Generic;
using System.Text;
// 当定义两种或多种具有相同名称的方法时,就称作重载。
namespace MethodTestOverride
{
    class TestOverride
    {
        static void Main(string[] args)
        {
            Console.WriteLine("a+b=" + Add(2, 3).ToString());
            Console.WriteLine("a+b+c=" + Add(2, 3, 5).ToString());
        }
        public static double Add(double a, double b)
        {
            return a + b;
        }
        public static double Add(double a, double b, double c)
        {
            return (a + b) + c;
        }
    }
}
32 、运算符的重载
using System;
using System.Collections.Generic;
using System.Text;
// 运算符重载
namespace OperatorOverride
{
    class OperatorOverride
    {
        double a, b;
        public OperatorOverride(double m, double n)
        {
            a = m;
            b = n;
        }
        public static OperatorOverride operator +(OperatorOverride x, OperatorOverride y)
        {
            OperatorOverride t = new OperatorOverride(0, 0);
            t.a = x.a + y.a;
            t.b = x.b + y.b;
            return t;
        }
        public void DisCom()
        {
            Console.WriteLine("{0}+{1}", a, b);
        }
        static void Main(string[] args)
        {
            OperatorOverride xx = new OperatorOverride(2, 1);
            xx.DisCom();
            OperatorOverride yy = new OperatorOverride(3, 4);
            yy.DisCom();
            OperatorOverride zz = new OperatorOverride(0, 0);
            zz = xx + yy;
            zz.DisCom();
        }
    }
}
继承
33 、实现继承
using System;
using System.Collections.Generic;
using System.Text;
//C# 语言提供了两种实现继承方式:实现继承和接口继承。实现继承只允许单一继承,接口可以实现多接口继承。
namespace jicheng
{
    class Program
    {
        static void Main(string[] args)
        {
            ChildClass cc = new ChildClass();
            cc.a = 5;
            cc.b = 2;
            cc.ShowAB(); // 直接可以使用基类中成员方法
            Console.WriteLine("a+b=" + cc.Add());
        }
    }
    class BaseClass
    {
        public double a, b;
        public void ShowAB()
        {
            Console.WriteLine("a=" + a + " b=" + b);
        }
    }
    class ChildClass : BaseClass        // 继承基类BassClass
    {
        public double Add()
        {
            return a + b;   // 使用基类中成员变量
        }
    }
}
接口
34、接口
using System;
using System.Collections.Generic;
using System.Text;
// 接口定义了类要实现的一系列方法,而他自己并不实现这些方法,只是以逻辑结构方式来描述类所提供一系列没有具体实现的方法。
namespace Interface
{
    class Program
    {
        static void Main(string[] args)
        {
            CNumber number = new CNumber();
            Console.WriteLine(" 执行getNext()方法在原数值上加" );
            for (int i = 0; i < 5; i++)
                Console.WriteLine(" " + number.getNext());
            number.reSet();
            Console.WriteLine(" 执行reSet()方法重置初始值为及getNext()方法" );
            for (int j = 0; j < 5; j++)
                Console.WriteLine(" " + number.getNext());
            Console.WriteLine(" 执行setStart()方法设置初始值为并执行getNext()方法" );
            number.setStart(100);
            for (int k = 0; k < 5; k++)
                Console.WriteLine(" " + number.getNext());
        }
    }
    public interface INumber // 实现接口
    {
        int getNext();        // 返回系列中的下一个数字
        void reSet();         // 重新开始
        void setStart(int x); // 设置初始值
    }
    class CNumber : INumber    // 接口INumber
    {
        int start;
        int val;
        public CNumber()
        {
            start = 0;
            val = 0;
        }
        public int getNext() // 完善方法getNext()
        {
            val += 5;
            return val;
        }
        public void reSet()   // 完善方法reSet()
        {
            start = 0;
            val = 0;
        }
        public void setStart(int x) // 完善方法setStart(int x)
        {
            start = x;
            val = x;
        }
    }
}
索引指示器
35、索引指示器
using System;
using System.Collections.Generic;
using System.Text;
// 索引指示器可以像数组那样对对象使用下标。它提供了通过索引方式方便地访问类的数据信息方法。
namespace IndexerDeciarator
{
    class Program
    {
        static void Main(string[] args)
        {
            Tream tream = new Tream();
            tream[1] = "one";
            tream[3] = "three";
            tream[4] = "five";
            for (int i = 0; i < 6; i++)
                Console.WriteLine(i + ":" + tream[i]);
        }
    }
    class Tream
    {
        string[] str_Name = new string[6];
        public string this[int nIndex]
        {
            get { return str_Name[nIndex]; }
            set { str_Name[nIndex] = value; }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值