标签:
1 函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
/*static void Main(string[] args)
{
int[] myArray = {1,5,3,7,8,22,21,33,35,99,26 };
int maxVal = MaxValue(myArray);
Console.WriteLine("The maximum value in myArray is {0}",maxVal);
Console.ReadKey();
}
static int MaxValue(int[] intArray)
{
int maxVal =intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
{
maxVal=intArray[i];
}
}
return maxVal;
}*/
static int SumVals(params int[] vals) //关键字params
{
int sum = 0;
foreach (int val in vals)
{
sum += val;
}
return sum;
}
static void Main(string[] args)
{
int sum = SumVals(1, 2, 3, 4, 5);
Console.WriteLine("The sum={0}", sum);
Console.ReadKey();
} }
}
2 类,方法,属性
1不加static的方法必须将类进行实例化再可以进行调用,而加static的方法可以直接使用
2 产生对象必须通过new关键字产生对象
Eg: person minjie=new person();
3 c#中的访问修饰符 有public 在任何地方被访问
Private 只能在本类中被访问
Protected 只能在本类中和子类中被访问
Intemal 只能在本项目中被访问
4 使用构造方法的好处
对多个属性进行赋值时,不需要重复写实例名
可以保证用户在new一个对象的时候必须对某一个属性进行赋值
和第2类似,在创建对象时,对只读属性进行初始化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
/*person p1 = new person(); //定义对象时和c++不同的地方
p1.name = "panpan";
p1.age = 20;
p1.Height = 162;*/
/*person2 p1 = new person2();
p1.age = 20;
p1.Height = 162;
p1.Givename("panpan");
p1.SayHello();*/
/*person3 p1 = new person3();
p1.Age = 30;
Console.WriteLine("年龄是{0}",p1.Age);
p1.Age = -100;
Console.WriteLine("年龄是{0}", p1.Age);
p1.Age = -1;
Console.WriteLine("年龄是{0}", p1.Age);*/
person4 p1 = new person4();
p1.Age = 30;
Console.WriteLine(p1.Age);
Console.ReadKey();
}
}
class person4
{
private int age;
public int Age
{
set
{
this.Age = value;//进入死循环状态,注意!
}
get
{
return this.age;
}
}
}
class person3
{
private int age;
public int Age
{
set
{
if(value<0)
{
return ;
}
this.age=value;
}
get
{
return this.age;
}
}
}
class person2
{
public int Height;
private string name;
public int age;
public void SayHello()
{
Console.WriteLine("hello,我是{0},我的年龄是{1},我的身高是{2}", this.name, this.age, this.Height);
}
public void Givename(string name)
{
this.name = name;
}
}
class person
{
public int Height;
public int age;
public string name;
public void SayHello()
{
Console.WriteLine("hello,我是{0},我的年龄是{1},我的身高是{2}",this.name,this.age,this.Height);
}
}
}
例子:
using System;
class Address
{
public string name;
public string address;
}
class Methodparams
{
public static void Main()
{
string myChoice;
Methodparams mp = new Methodparams();
do
{
myChoice = mp.getchoice();
mp.makeDecision(myChoice);
Console.Write("Press any key to continue...");
Console.ReadLine();
Console.WriteLine();
} while (myChoice != "Q" && myChoice != "q");
}
string getchoice()
{
string mychoice;
Console.WriteLine("My Address Book\n");
Console.WriteLine("A-Add New Address");
Console.WriteLine("D-Delete Address");
Console.WriteLine("M-Modify Address");
Console.WriteLine("V-View Addresses");
Console.WriteLine("Q-Quit\n");
Console.WriteLine("Choice(A,D,M,V,Q)");
mychoice = Console.ReadLine();
return mychoice;
}
void makeDecision(string myChoice)
{
Address addr = new Address();
switch (myChoice)
{
case "A":
case "a":
addr.name = "Joe";
addr.address = "C# station";
this.addAddress(ref addr);
break;
case "D":
case"d":
addr.name="Robert";
this.deleteAddress(addr.name);
break;
case "M":
case "m":
addr.name="Matt";
this.modifyAddress(out addr);
Console.WriteLine("Name is now {0}",addr.name);
break;
case "V":
case "v":
this.viewAddress("Cheryl","Joe","Matt","Robert");
break;
case "Q":
case "q":
Console.WriteLine("Bye");
break;
}
}
void addAddress(ref Address addr)
{
Console.WriteLine("Name:{0},Address:{1} added.",addr.name,addr.address);
}
void deleteAddress(string name)
{
Console.WriteLine("You wish to delete {0}‘s address.",name);
}
void modifyAddress(out Address addr)
{
addr = new Address();
addr.name = "Joe";
addr.address = "C# Station";
}
void viewAddress(params string[] names)
{
foreach (string name in names)
{
Console.WriteLine("Name:{0}",name);
}
}
}
3 名称空间
using System;
namespace csharp_station
{
namespace tutorial
{
class myexmaple1
{
public static void myprint1()
{
Console.WriteLine("First Example of calling another namespace menber.");
}
}
}
class NamespaceCalling
{
public static void Main()
{
tutorial.myexmaple1.myprint1();
csharp_station.tutorial.myexmaple2.myprint2();
Console.ReadKey();
}
}
}
namespace csharp_station.tutorial //与上面嵌套命名空间相同的命名空间
{
class myexmaple2
{
public static void myprint2()
{
Console.WriteLine("Second Example of calling another namespace menber.");
}
}
}
4 类
例子1
using System;
class OutputClass
{
string mystring;
public OutputClass(string inputstring)
{
mystring = inputstring;
}
public void printstring()
{
Console.WriteLine("{0}",mystring);
}
~OutputClass()
{ }
}
class Exampleclass
{
public static void Main()
{
OutputClass outC1 = new OutputClass("This is printed by the outputclass.");
outC1.printstring();
Console.ReadKey();
}
}
例子2
using System;
public class parentclass
{
public parentclass()
{
Console.WriteLine("parent constructor.");
}
public void print()
{
Console.WriteLine("I‘m a parent class");
}
}
public class childclass : parentclass
{
public childclass()
{
Console.WriteLine("Child constructor.");
}
public static void Main()
{
childclass child = new childclass();
child.print();
Console.ReadKey();
}
例子3
using System;
public class parent
{
string parentstring;
public parent()
{
Console.WriteLine("parent constructor.");
}
public parent(string mystring)
{
parentstring=mystring;
Console.WriteLine(parentstring);
}
public void print()
{
Console.WriteLine("I‘m a parent class");
}
}
public class child : parent
{
public child():base("Form Derived")
{
Console.WriteLine("Child constructor.");
}
public void print()
{
base.print();
Console.WriteLine("I am a child class:");
}
public static void Main()
{
child child = new child();
child.print();
((parent)child).print();
Console.ReadKey();
}
}
5 多态
如果子类和父类的方法名重名了,会报绿线,如何把绿线干掉?
第一:我们可以在子类的方法的访问修饰符后面加new;
第二:我们可以在父类的方法上加一个virtual 然后 子类在继承父类的时候,可以用override重写父l类的方法
6 接口(interface)
注:1 接口中可以有属性,方法
2 接口的名称通常以“I”开头,如IList
3 如果一个类即继承了类又实现了接口,那么类必须写在前面。一个类只能继承一个父类,可以实现多个接口
Eg:class Student:Person,Ifly,ISwim
标签:
原文地址:http://www.cnblogs.com/panpan-tostring/p/4640617.html