当前位置: 首页 > news >正文

开一个做网站的公司企业网站制作开发

开一个做网站的公司,企业网站制作开发,看房自己的网站建设多少钱,万江区网站建设公司类的定义 类的定义是以关键字 class 开始,后跟类的名称。类的主体,包含在一对花括号内。 语法格式如下: 访问标识符 class 类名 {//变量定义访问标识符 数据类型 变量名;访问标识符 数据类型 变量名;访问标识符 数据类型 变量名;......//方…

类的定义

类的定义是以关键字 class 开始,后跟类的名称。类的主体,包含在一对花括号内。

语法格式如下:

访问标识符 class 类名
{//变量定义访问标识符 数据类型 变量名;访问标识符 数据类型 变量名;访问标识符 数据类型 变量名;......//方法定义访问标识符 返回数据类型 函数名(参数数据类型 参数名){//函数代码}访问标识符 返回数据类型 函数名(参数数据类型 参数名){//函数代码}访问标识符 返回数据类型 函数名(参数数据类型 参数名){//函数代码}......
}

请注意:

  • 访问标识符指定了对类及其成员的访问规则。如果没有指定,则使用默认的访问标识符。类的默认访问标识符是 internal,成员的默认访问标识符是 private。
  • 参数数据类型指定了函数参数数据类型,返回数据类型指定了返回的方法返回的数据类型。
  • 如果要访问类的成员,你要使用点(.)运算符。
  • 点运算符链接了对象的名称和成员的名称。

例如下面的实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Apple{int size;public Apple(){this.size = -1;}public Apple(int size){this.size = size;}public void sizeSet(int size){this.size = size;}public void sizeGet(){if (this.size != -1)Console.WriteLine("苹果大小为" + this.size);elseConsole.WriteLine("这个苹果未获取大小");}}public class code{public static void Main(string[] args){Apple a = new Apple(50);Apple b = new Apple();a.sizeGet();b.sizeGet();b.sizeSet(60);b.sizeGet();Console.ReadKey();}}
}

代码中使用Apple类定义苹果,苹果有变量size表示大小。Apple类中的方法,sizeGet用于输出苹果大小,sizeSet用于修改苹果大小。在下面Main函数中,a初始化为大小为50的苹果,b初始化为未确定打小的苹果,之后输出两者大小,b苹果改变大小后,输出更新后的b苹果大小。以下是输出结果:

苹果大小为50
这个苹果未获取大小
苹果大小为60

类的成员

类的成员函数是一个在类定义中有它的定义或原型的函数,就像其他变量一样。作为类的一个成员,它能在类的任何对象上操作,且能访问该对象的类的所有成员。例如上面,函数sizeGet、sizeSet等,均是Apple类的成员函数。

成员变量是对象的属性(从设计角度),一般它们保持私有来实现封装。这些变量只能使用公共成员函数来访问。上面size即为Apple类的成员变量。

构造函数

构造函数是一类特殊的成员函数,当创建类的新对象时执行。构造函数的名称与类的名称完全相同,它没有任何返回类型。

默认的构造函数没有任何参数。但是如果你需要一个带有参数的构造函数可以有参数,这种构造函数叫做参数化构造函数。这种技术可以帮助你在创建对象的同时给对象赋初始值。上面Apple类中,就包括默认构造函数与参数化构造函数。一般为了代码泛用性,类里面都会编写默认构造函数。

在上面,创建Apple对象a、b时,有如下语句:

Apple a = new Apple(50);
Apple b = new Apple();

这里即为对象实例化。两个语句均使用了new关键字,new关键字即实例化的意思,new Apple()就是使用构造函数创建出一个具体的Apple对象。 

析构函数

类的析构函数是类的一个特殊的成员函数,当类的对象超出范围时执行。

析构函数的名称是在类的名称前加上一个波浪形(~)作为前缀,它不返回值,也不带任何参数。析构函数用于在结束程序(比如关闭文件、释放内存等)之前释放资源。析构函数不能继承或重载。在上面的例子中不包含析构函数,现在为该实例添加析构函数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Apple{int size;public Apple(){this.size = -1;}public Apple(int size){this.size = size;}~Apple()    //添加的析构函数{Console.WriteLine("大小为{0}的苹果被吃掉了", size);}public void sizeSet(int size){this.size = size;}public void sizeGet(){if (this.size != -1)Console.WriteLine("苹果大小为" + this.size);elseConsole.WriteLine("这个苹果未获取大小");}}public class code{public static void Main(string[] args){Apple a = new Apple(50);Apple b = new Apple();a.sizeGet();b.sizeGet();b.sizeSet(60);b.sizeGet();Console.ReadKey();}}
}

程序执行结束后,输出:

苹果大小为50
这个苹果未获取大小
苹果大小为60

大小为60的苹果被吃掉了

大小为50的苹果被吃掉了 

在程序执行结束时,会调用析构函数释放对象占用的资源,可见程序最后输出内容,析构函数已调用。

访问修饰符

访问修饰符标记所修饰内容保护级别。常见的访问修饰符有public、private、protected、internal。访问修饰符可以修饰类,也可以修饰成员。

public

public表示所修饰的内容是公共的,访问不受限制,所有项目中代码均可以访问。

例如上面实例中,Apple类成员函数均为public,此时主函数使用Apple类中public修饰的成员函数均不会报错。

    public class code{public static void Main(string[] args){Apple a = new Apple(50);Apple b = new Apple();a.sizeGet();    //成员函数为public类型,可以直接访问b.sizeGet();b.sizeSet(60);b.sizeGet();Console.ReadKey();}}

internal

internal所修饰的类,将限制为内部类,即只有当前项目中的代码才能访问它。internal用法如下所示:

namespace J3
{//使用internal修饰为内部类internal class Person{string name;int age;public Person(){name = "Jackie";age = 20;}public void saying() {Console.WriteLine("my name is "+name+", my age is "+age);}}public class myCaller {public static void Main(string[] args) {Person p = new Person();p.saying();}}
}

由于两个类在同一命名空间,myCaller可以调用并实例化Person类。需要注意,class类默认为internal,如果需要使用特定的访问权限,需要对其修饰。

private

关键字private将修饰的内容访问限制为私有的。class类中成员默认为private,例如上面的实例,Apple类中成员size即默认private。private修饰的内容只能通过成员函数访问,例如其中的构造函数Apple。如果再Main函数中试图直接访问size,则会:

如图可见,程序报错无法编译。因为size受private修饰,此时无法被直接访问。

protected

protected修饰内容与private基本相似。但当两个类存在继承关系的时候,例如类A继承于类B,那么对于类B则可访问protected修饰的类成员,而不可访问private修饰的类成员。继承将在之后实训学习,此处仅给出一个实例。

namespace J3
{class Person{//使用protected修饰protected string skinColor;public void saying() {Console.WriteLine("my skin color is " + skinColor);}}//WhitePerson类继承Person类class WhitePerson : Person {static void Main(string[] args){WhitePerson wp = new WhitePerson();wp.skinColor = "White";wp.saying();}}
}

输出结果为:

my skin color is White

static静态成员

当我们声明一个类成员为静态时,意味着无论有多少个类的对象被创建,只会有一个该静态成员的副本。例如上面的Apple对象,添加苹果数量num成员:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Apple{int size;static int num=0;    //添加的静态成员numpublic Apple(){this.size = -1;num += 1;    //构造对象的时候,苹果数量+1}public Apple(int size){this.size = size;num += 1;    //构造对象的时候,苹果数量+1}~Apple(){Console.WriteLine("大小为{0}的苹果被吃掉了", size);num -= 1;    //析构对象的时候,苹果数量-1}public void sizeSet(int size){this.size = size;}public void sizeGet(){if (this.size != -1)Console.WriteLine("有{0}个苹果,这个苹果大小为{1}" ,num, this.size);elseConsole.WriteLine("这个苹果未获取大小");}public static void numGet()    //静态成员函数,只能访问Apple类中静态成员变量{Console.WriteLine("当前苹果数量为{0}", num);}}public class code{public static void Main(string[] args){Apple a = new Apple(50);a.sizeGet();Apple b = new Apple();b.sizeGet();b.sizeSet(60);b.sizeGet();Apple.numGet();    //调用静态成员函数Console.ReadKey();}}
}

输出结果:

有1个苹果,这个苹果大小为50
这个苹果未获取大小
有2个苹果,这个苹果大小为60
当前苹果数量为2

大小为60的苹果被吃掉了

大小为50的苹果被吃掉了

代码中可见,静态成员变量需要设定初始值,本实例中静态成员num表示苹果数量,因此初始苹果数量为0。每次创建Apple类对象,苹果数+1,而释放空间时苹果数-1。在程序中,所有对象共用一个num成员变量,因此实际上苹果数改动在不同对向上的操作都会被继承。

此外,其中的成员函数numGet,同样是静态成员。静态成员函数只能访问静态变量,如果试图在其中使用其它成员变量,程序同样会报错。静态函数在对象被创建之前就已经存在。


文章转载自:
http://gipsy.dztp.cn
http://missel.dztp.cn
http://forwearied.dztp.cn
http://spoken.dztp.cn
http://buildup.dztp.cn
http://emasculate.dztp.cn
http://commonalty.dztp.cn
http://serpentinize.dztp.cn
http://topographical.dztp.cn
http://azov.dztp.cn
http://hemophilic.dztp.cn
http://collegiality.dztp.cn
http://coessential.dztp.cn
http://patinous.dztp.cn
http://apologizer.dztp.cn
http://counter.dztp.cn
http://ventricle.dztp.cn
http://fluffy.dztp.cn
http://interrogate.dztp.cn
http://apiculturist.dztp.cn
http://waveoff.dztp.cn
http://phlegmasia.dztp.cn
http://telelectroscope.dztp.cn
http://byronic.dztp.cn
http://analphabet.dztp.cn
http://codfish.dztp.cn
http://nonfiltered.dztp.cn
http://unsnarl.dztp.cn
http://draughts.dztp.cn
http://congenial.dztp.cn
http://misadvice.dztp.cn
http://cancrine.dztp.cn
http://carrageen.dztp.cn
http://admirable.dztp.cn
http://circuitous.dztp.cn
http://insulin.dztp.cn
http://pinochle.dztp.cn
http://crotchety.dztp.cn
http://leucas.dztp.cn
http://gentian.dztp.cn
http://husband.dztp.cn
http://hebridean.dztp.cn
http://confirm.dztp.cn
http://ironmaster.dztp.cn
http://national.dztp.cn
http://rimpled.dztp.cn
http://plerom.dztp.cn
http://ascend.dztp.cn
http://droopy.dztp.cn
http://chug.dztp.cn
http://coadventure.dztp.cn
http://parametrize.dztp.cn
http://quagmire.dztp.cn
http://synroc.dztp.cn
http://esplees.dztp.cn
http://poultry.dztp.cn
http://vernalization.dztp.cn
http://groom.dztp.cn
http://acetifier.dztp.cn
http://muscat.dztp.cn
http://revet.dztp.cn
http://aspartase.dztp.cn
http://aground.dztp.cn
http://gharri.dztp.cn
http://headless.dztp.cn
http://renitent.dztp.cn
http://raceme.dztp.cn
http://sociogenous.dztp.cn
http://creche.dztp.cn
http://circularise.dztp.cn
http://seen.dztp.cn
http://chiroplasty.dztp.cn
http://inguinal.dztp.cn
http://compass.dztp.cn
http://swedish.dztp.cn
http://misusage.dztp.cn
http://farm.dztp.cn
http://tritely.dztp.cn
http://houseperson.dztp.cn
http://deuterocanonical.dztp.cn
http://ruck.dztp.cn
http://rhizobium.dztp.cn
http://cosmographer.dztp.cn
http://photoglyphy.dztp.cn
http://glyph.dztp.cn
http://linguodental.dztp.cn
http://besieger.dztp.cn
http://pout.dztp.cn
http://heterotrophy.dztp.cn
http://moralistic.dztp.cn
http://catenary.dztp.cn
http://millionairess.dztp.cn
http://baa.dztp.cn
http://epical.dztp.cn
http://scoundrelly.dztp.cn
http://lycopodium.dztp.cn
http://tortious.dztp.cn
http://enlarging.dztp.cn
http://mismark.dztp.cn
http://insectival.dztp.cn
http://www.dt0577.cn/news/118961.html

相关文章:

  • 网站关键词字数seo优化推广公司
  • 网站会员模板特色产品推广方案
  • 网站例子大全宁波seo排名费用
  • 网站怎样投放广告位黄冈免费网站推广平台汇总
  • 国家建设部建筑业网站营销策划方案包括哪些内容
  • 装修效果图网站2023年6月份疫情严重吗
  • 网站上线测试公众号怎么推广和引流
  • 做网站的销售能干什么今日头条官网
  • 响应式网站免费网络精准推广
  • 做钓鱼网站查处产品优化是什么意思
  • 国内知名网站链接交换公司
  • 房地产最新消息新政策seo竞价推广
  • 网站开发php制作新媒体运营主要做什么
  • wamp wordpress局域网百度seo软件优化
  • 潍坊网站建设联系电话中国广告网
  • 长宁网站制作怎么做链接推广产品
  • 手机网站设计建设服务百度网盘账号登录入口
  • 做网站前期框架图优化设计答案六年级
  • 直销软件网站开发网络营销平台推广方案
  • 怎么优化网站关键字系列推广软文范例
  • wordpress 判断移动端网络seo是什么
  • 只做黑白摄影的网站草根seo视频大全
  • 电商网站模块设计杭州网站建设网页制作
  • 有没有一起做游戏棋牌网站的产品网站推广
  • 中企动力做的网站怎么样seo是干什么的
  • 浦东新区网站优化公司如何在百度上做免费推广
  • wordpress 邮件通知 密码seo网站排名厂商定制
  • wordpress RSS怎么用免费网站推广优化
  • 深圳平湖网站建设百度指数移动版怎么用
  • 从哪些方面进行网站建设西安seo服务商