zoukankan      html  css  js  c++  java
  • C#代码用法

    1、new的用法
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace yanz
    {
    public class student
    {
    public string strName;
    public student(string _strName)
    {
    this.strName=_strName;
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    student s=new student("张三");
    student t=new student("李四");
    Console.WriteLine(t.strName);
    Console.WriteLine(s.strName);
    Console.ReadLine();
    }
    }
    }
    Student为类,“student s=new student("张三”);"中“s”代表张三

    2、引用参数
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace ConsoleApplication2
    {
    class myclass
    {
    public void Swap(ref int x,ref int y)
    {
    int k;
    k=x;
    x=y;
    y=k;
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    int a=8;b=10;
    Console.WriteLine("a={0},b={1}",a,b);
    myclass mycl=new myclass();
    mycl.Swap(ref a,ref b);
    Console.WriteLine("a={0},b={1}",a,b);
    }
    }
    }

    3、方法重载
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace ConsoleApplication1
    {
    class student
    {
    public string strname;
    public int nage;
    public void grow()
    {
    this.nage++;
    }
    public void grow(int _nagespan)
    {
    this.nage +=_nagespan;
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    student s=new student();
    s.strname="zhangsan";
    s.nage=20;
    s.grow();
    Console.WriteLine(s.nage);
    s.grow(3);
    Console.WriteLine(s.nage);
    Console.ReadLine();
    }
    }
    }
    4、方法的调用
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace Example_ConstructOverload
    {
    class Time
    {
    public int nHour,nMinute,nSecond;
    public Time()
    {
    nHour=nMinute=nSecond=0;
    }
    public Time(int Hour)
    {
    nHour=Hour;
    nMinute=nSecond=0;
    }
    public Time(int Hour,int Minute)
    {
    nHour=Hour;
    nMinute=Minute;
    nSecond=0;

    }
    public Time(int hour,int Minute,int Second)
    {
    nHour=hour;
    nMinute=Minute;
    nSecond=Second;
    }
    }
    class Test
    {
    static void Main()
    {
    Time t1,t2,t3,t4;
    t1=new Time();
    t2=new Time(10);
    t3=new Time(10,20);
    t4=new Time(10,22,30);
    Console.WriteLine("t1的时间为:{0}时{1}分{2}秒",t1.nHour,t1.nMinute,t1.nSecond);
    Console.WriteLine("t2的时间为:{0}时{1}分{2}秒:",t2.nHour,t2.nMinute,t2.nSecond);
    Console.WriteLine("t3的时间为:{0}时{1}分{2}秒:",t3.nHour,t3.nMinute,t3.nSecond);
    Console.WriteLine("t4的时间为:{0}时{1}分{2}秒:",t4.nHour,t4.nMinute,t4.nSecond);
    Console.ReadLine();
    }
    }
    }

  • 相关阅读:
    一则线上MySql连接异常的排查过程
    有一种娱乐叫看别人编程
    程序员DNS知识指南
    中国式开源
    RSS与公众号
    论国人的素质和一个公司的商业道德
    《阿里游戏高可用架构设计实践》阅读笔记
    《淘宝数据魔方技术架构解析》阅读笔记
    软件体系架构_系统质量属性场景描述_结合《淘宝网》实例
    《余额宝技术架构及演进》阅读笔记
  • 原文地址:https://www.cnblogs.com/zzp0320/p/6945534.html
Copyright © 2011-2022 走看看