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();
}
}
}
C#代码用法的更多相关文章
- js跳转页面代码用法
一:window.location.href='https://www.baidu.com'; 需要加上http或者https,否则会查找项目内htm打开. 二:window.history.bac ...
- godot新手教程2[godot常用代码用法]
Godot概念: 在godot内,使用的语言是GDScript,大部分代码风格是和python一样. 在GDScript内代码段结束是换到下一行即可,不需要也不能添加”;”号,(注意:代码段结束后不能 ...
- discuz!代码内置颜色大全(收藏)
加闪烁字:[light]文字[/light] 加文字特效:[shadow=255,red,2]文字[/shadow]: 在标签的中间插入文字可以实现文字阴影特效,shadow内属性依次为宽度.颜色和边 ...
- 2-Qt关闭子窗口时执行特定代码
https://blog.csdn.net/naibozhuan3744/article/details/82689434 本文主要总结在关闭qt的QWidget子窗口瞬间,执行特定代码.由于主窗口关 ...
- log4j 简单用法
maven添加必要库: <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <gro ...
- Less的用法
Less常用来写样式,比较多的用法是使用第三方软件编译成CSS文件,然后在HTML页面引入CSS文件.而不是直接在HTML页面里引入编译文件和Less文件.如此以来,在后期修改方便的多.当然,在写小项 ...
- [C/C++]快速读入代码(快读)
快读 1.为什么要有快读 好吧,有些题目看上去十分简单,例如https://www.luogu.com.cn/problem/P4305这道题,实际上数据量巨多,光是一个测试点就可能有几个MB,在这种 ...
- Python基础之函数:6、异常相关和生成器对象、yield用法、生成器表达式
目录 一.异常常见类型 1.类型错误 2.缩进错误 3.索引错误 4.语法错误 5.属性错误 6.key键错误 二.异常处理语法结构 1.基本语法结构 2.查看错误类型 3.针对不同类型所作措施 4. ...
- CanvasWebgl项目介绍
CanvasWebgl 介绍 CanvasWebgl 是一个基于webgl 开发的2d绘图框架,使用TypeScript开发 CanvasWebgl的功能,是在屏幕空间 或者 3D空间产生一个画布 ...
随机推荐
- Optional int parameter 'rows' is not present but cannot be translated into a null value due to being declared as a primitive type.
我的spring mvc 代码: @Controller @RequestMapping("/product") public class Fancy { @RequestMapp ...
- python--websocket数据解析
# websocket实现原理 ''' 1.服务端开启socket,监听ip和端口 2.客户端发送连接请求(带上ip和端口) 3.服务端允许连接 4.客户端生成一个随机字符串,和magic strin ...
- 多线程之:ThreadLocal
Java中ThreadLocal类可以使创建的变量只被同一个线程进行读和写操作,即使有多个线程同时执行同一段代码,并且这段代码中又有一个指向同一个ThreadLocal变量的引用,这些线程依然不能看到 ...
- hdu 畅通工程系列题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 并查集水. #include <stdio.h> #include <iost ...
- laravel中建立公共视图的方法
1.用法概要 @include('common.header') 包含子视图 @extends('article.common.base') 继承基础模板 @yield('content') 视图占位 ...
- Python的程序结构[3] -> 变量/Variable[1] -> LEGB 法则
LEGB法则 / LEGB Rule LEGB 变量搜索顺序 ---- 来源于<Python学习手册>/ Learning Python Page 419 ---- Python 的变量名 ...
- 洛谷——P2067 Cytus-Holyknight
P2067 Cytus-Holyknight 题目背景 本人最初作 以此纪念伟大的ios.安卓.PSV平台音乐游戏<cytus> 后续将不断更新. -------------Chapter ...
- lua异常捕获
解析json失败,想要捕获异常,可以使用pacll local cjson = require("cjson") local str = '[{"name":& ...
- 1.12(java学习笔记)多态及向上、向下转型
一.多态 多态是指同一个方法被调用,由于对象不同导致行为不同. 例如调用自由活动方法,张三喜欢玩耍,那么他就会去玩耍. 李四喜欢学习,那么他可能去学习.调用方法因对象的不同 而产生了不同的行为. 形成 ...
- NOI2016 高中OI生涯的最后一站
你乘坐的航班XXX已经抵达终点站——四川绵阳. “呼——”机舱外的天空灰沉沉的,不禁有些压抑与紧张. 一出机场,就看见南山中学的牌子,黄色衣服的志愿者们,还有热情的老师们. 感觉刚才的情绪又一扫而空了 ...