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();
    }
    }
    }

  • 相关阅读:
    教你如何去除电脑QQ聊天窗口上的广告?
    Web 通信技术 ——跨文档信息传输(JavaScript)
    JavaScript中如何给按钮设置隐藏与显示属性
    JavaScript中的innerHTML属性的使用
    JavaScript中点击按钮弹出新的浏览器窗口
    JavaScript中prompt的使用
    考试报名管理系统
    Python——用turtle模块画海龟的第一步
    Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
    用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
  • 原文地址:https://www.cnblogs.com/zzp0320/p/6945534.html
Copyright © 2011-2022 走看看