c#入门学习之 面向对象学习

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

时间: 2024-08-26 20:44:11

c#入门学习之 面向对象学习的相关文章

python学习之面向对象学习

一.什么事面向对象,以及面向对象的优点? 面向过程优缺点: 我们知道面向过程是流水线式的编程,过程就是解决问题的步骤,把大的问题化解成小的模块 面向过程优点: 极大的降低了程序的复杂度 面向过程缺点: 牵一发而动全身, 所以完成一个模块很少改动,否则改动的地方比较多 面向对象优缺点: 面向对象编程的核心是对象,由属性和函数构成 面向对象优点: 解决程序的扩展性,对某个类的修改能反映到整个体系中 类的语法结构: class 类名:     类体      例子: class People:    

python学习之面向对象学习进阶

一.property属性高级用法 可以将对象方法作为属性使用 例子: class Foo:     def __init__(self, age):         )        self.__age = age     @property     def age(self):         return self.__age     @age.setter     def age(self, args):         self.__age = args     @age.delete

JavaScript 面向对象学习——1

公司项目采用Ext,结果本人发现入门不易!尤其是采用JavaScript编写面向对象程序,经常使用jQuery的知道,jQuery是面向函数编程的,所以很容易入门.然而,Ext是面向对象的,那么,当你想要自定义Ext组件的时候,或者使用Ext组件的时候就会很苦恼.所以,要先学习Javascript面向对象基础,其次查看Ext源代码. 这将是后期的学习路线,博客路线. 1 Javascript是基于原型(Prototype based)的面向对象的语言,Java语言,是基于类模式(Class ba

Spring入门IOC和AOP学习笔记

Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容器中Bean之间的依赖关系,使用一种叫做"依赖注入"的方式来管理bean之间的依赖关系. Spring有两个核心接口:BeanFactory和ApplicationContext,ApplicationContext是BeanFactory的子接口.它们都可以代表Spring容器,Spri

一个入门rpc框架的学习

一个入门rpc框架的学习 参考 huangyong-rpc 轻量级分布式RPC框架 该程序是一个短连接的rpc实现 简介 RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样. RPC 可基于 HTTP 或 TCP 协议,Web Service 就是基于 HTTP 协议的 RPC, 它具有良好的跨平台性,但其性能却不如基于 TCP 协议的 RPC.会两方面会直接影响 RPC 的性能,一是传输方式,二是序列化. 众所

面向对象学习笔记

abstract:抽象类或方法的关键字注意事项:一个类中至少有一个抽象方法不允许有大括号主要意义就是规范方法,要求必须按照抽象中的方法来写继承抽象类的类,必须重载完全抽象类的方法抽象类是不能被实例化的,因为本身没有意义 extends 继承的关键字 类中常用的关键字:final 被定义的类不能被继承,该类的方法不能被继承,相当于一个锁的功能self self::静态成员,相当于类名称::静态成员,和this是有区别的,$this访问类中的内容必须实例化,而self是直接可以访问类中的内容的,多用

php面向对象学习

php面向对象学习 近期跟着别人开发一套php程序,深感自己面向对象很白痴,于是再次巩固了一下面向对象的学习,自己整理了一下这几天面向对象的东西,给大家分享!! 面向对象的三大特性: 封装 -- 隐藏内部实现,稳定外部接口 继承 -- 子类继承父类成员,实现代码复用 多态 -- 不同子类对同一消息做出不同的反映 一.接口 -- 是一套规范,遵守这个规范就可以实现功能 在PHP中,接口同样是一种规范和标准,可以约束类的行为,定义一个接口不指定具体的实现. 接口是把隐式公共方法和属性组合起来,以封装

2015 IOS 学习笔记 面向对象 初始化方法 ——蓝懿教育

今天学习了面向对象以及初始化方法,这个在实际应用中比较重要,也比较抽象,所以要具体在实例中才能理解. ————————面向对象有三大特性—————— 一.封装 封装是对象和类概念的主要特性.它是隐藏内部实现,稳定外部接口,可以看作是“包装”.封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏. 好处:使用更简单变量更安全可以隐藏内部实现细节开发速度加快 OC中一个类可以继承另一个类,被继承的类成为超类(superclass),继承的

大话设计模式学习笔记——面向对象基础

前言 好记性不如烂"笔头"系列--大话设计模式学习笔记 目录 面向对象基础 面向对象基础 什么是类与实例 一切事物皆为对象,即所有的东西老师对象,对象就是可以看到.感觉到.听到.触摸到.尝到.或闻到的东西.准确地说,对象是一个自包含的实体,用一组可识别的特性和行为来标识.面向对象编程,英文叫 Object-Oriented Programming,其实就是针对对象来进行编程的意思.类就是具有相同属性和功能的对象的抽象集合.实例就是一个真实的对象.比如我们属于'人'类,而个人就是'人'类