search
Homephp教程php手册Dive deep into C# class methods

Constructor example1: static void Main( string [] args){ SE engineer = new SE(); engineer.Age = 25; enginner.Name = Ai Biancheng; // Omit other attribute assignment operations Console.WriteLine( engineer.SayHi()); } We know that to use the attributes and methods of a class, we must first instantiate the class,

Constructor

example1:

<span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span><span style="color: #000000"> [] args)
{
    SE engineer</span>=<span style="color: #0000ff">new</span><span style="color: #000000"> SE();
    engineer.Age</span>=<span style="color: #800080">25</span><span style="color: #000000">;
    enginner.Name</span>=<span style="color: #800000">"</span><span style="color: #800000">艾边成</span><span style="color: #800000">"</span><span style="color: #000000">;
    </span><span style="color: #008000">//</span><span style="color: #008000">省略其他属性赋值操作</span>
<span style="color: #000000">    Console.WriteLine(engineer.SayHi());
    
}</span>

We know that to use the attributes and methods of a class, we must first instantiate the class. In instance 1, create an SE object through SE engineer=new SE();. This method of creating a class instance is called a constructor.

In Example 1, the constructor is called to create an SE object and assign values ​​to its properties one by one. If no values ​​are assigned, the system will assign default values ​​to each field of the class.

As can be seen from Example 1, the constructor of a class is a special method in the class, which has the following characteristics

  1. The method name is the same as the class name
  2. No return value type
  3. Mainly complete the initialization work of the object.

here:

When we are doing development, we generally do not do things other than initializing instances of classes in the constructor, and do not try to call the constructor explicitly

No-argument constructor

Syntax:

<span style="color: #008000">//</span><span style="color: #008000">访问修饰符    类名()</span>
<span style="color: #000000">{
    </span><span style="color: #008000">//</span><span style="color: #008000">方法体</span>
}

Constructor with parameters

Syntax:

<span style="color: #008000">//</span><span style="color: #008000">访问修饰符    类名(参数列表)</span>
<span style="color: #000000">{
    </span><span style="color: #008000">//</span><span style="color: #008000">方法体</span>
}

Implicit constructor

When we do not explicitly define a class constructor in the class, the system will automatically define a parameterless constructor without a method body for us. This is the implicit constructor. It is worth noting that when we When you explicitly define the constructor of a class, the system will not define the implicit constructor of the class for us

Method overloading

Overloading of constructor

example:

<span style="color: #000000">Public Class SE
{
    </span><span style="color: #0000ff">string</span><span style="color: #000000"> id;
    </span><span style="color: #0000ff">string</span><span style="color: #000000"> name;
    </span><span style="color: #008000">//</span><span style="color: #008000">带参构造</span>
    <span style="color: #0000ff">public</span> SE(<span style="color: #0000ff">string</span> id,<span style="color: #0000ff">string</span><span style="color: #000000"> name)
    {
        </span><span style="color: #0000ff">this</span>.id=<span style="color: #000000">id;
        </span><span style="color: #0000ff">this</span>.name=<span style="color: #000000">name;    
    }
    </span><span style="color: #008000">//</span><span style="color: #008000">无参构造</span>
    <span style="color: #0000ff">public</span><span style="color: #000000"> SE()
    {

    }
    </span><span style="color: #008000">//</span><span style="color: #008000">省略SE类的其它代码</span>
}

It can be clearly seen from this code program that there are two constructors in the SE class with the same method name but different numbers of parameters. This method is method overloading.

From the above examples we can summarize the characteristics of method overloading

  1. Method names are the same
  2. Method parameter types are different or the number of parameters is different
  3. In the same class

It should be noted that methods with the same method name and parameter class table have different return value types and cannot be called method overloading.

Method overloading example

example:

Public <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span><span style="color: #000000"> [] args)
{
    Console.WriteLine(</span><span style="color: #800080">8</span><span style="color: #000000">);
    Console.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">Hello</span><span style="color: #800000">"</span><span style="color: #000000">);
}</span>

In the example we can see that the first WriteLine method accepts an int type parameter, and the second WriteLine method accepts a string type parameter. WriteLine() provides a variety of overloaded methods to meet various needs,


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
基于Java的机器视觉实践和方法介绍基于Java的机器视觉实践和方法介绍Jun 18, 2023 am 11:21 AM

随着科技的不断发展,机器视觉技术在各个领域得到了广泛应用,如工业自动化、医疗诊断、安防监控等。Java作为一种流行的编程语言,其在机器视觉领域也有着重要的应用。本文将介绍基于Java的机器视觉实践和相关方法。一、Java在机器视觉中的应用Java作为一种跨平台的编程语言,具有跨操作系统、易于维护、高度可扩展等优点,对于机器视觉的应用具有一定的优越性。Java

Go 语言中的方法是怎样定义和使用的?Go 语言中的方法是怎样定义和使用的?Jun 10, 2023 am 08:16 AM

Go语言是近年来备受青睐的编程语言,因其简洁、高效、并发等特点而备受开发者喜爱。其中,方法(Method)也是Go语言中非常重要的概念。接下来,本文就将详细介绍Go语言中方法的定义和使用。一、方法的定义Go语言中的方法是带有接收器(Receiver)的函数,它是一个与某个类型绑定的函数。接收器可以是值类型或者指针类型。用于接收者的参数可以在方法名

PHP文件下载方法及常见问题解答PHP文件下载方法及常见问题解答Jun 09, 2023 pm 12:37 PM

PHP是一个广泛使用的服务器端编程语言,它的许多功能和特性可以将其用于各种任务,包括文件下载。在本文中,我们将了解如何使用PHP创建文件下载脚本,并解决文件下载过程中可能出现的常见问题。一、文件下载方法要在PHP中下载文件,我们需要创建一个PHP脚本。让我们看一下如何实现这一点。创建下载文件的链接通过HTML或PHP在页面上创建一个链接,让用户能够下载文件。

Vue 中的 createApp 方法是什么?Vue 中的 createApp 方法是什么?Jun 11, 2023 am 11:25 AM

随着前端开发的快速发展,越来越多的框架被用来构建复杂的Web应用程序。Vue.js是流行的前端框架之一,它提供了许多功能和工具来简化开发人员构建高质量的Web应用程序。createApp()方法是Vue.js中的一个核心方法之一,它提供了一种简单的方式来创建Vue实例和应用程序。本文将深入探讨Vue中createApp方法的作用,其如何使用以及使用时需要了解

深入了解HTTP状态码100:它代表什么意思?深入了解HTTP状态码100:它代表什么意思?Feb 20, 2024 pm 04:15 PM

深入了解HTTP状态码100:它代表什么意思?HTTP协议是现代互联网应用中最为常用的协议之一,它定义了浏览器和Web服务器之间进行通信所需的标准规范。在HTTP请求和响应的过程中,服务器会向浏览器返回各种类型的状态码,以反映请求的处理情况。其中,HTTP状态码100是一种特殊的状态码,用来表示"继续"。HTTP状态码由三位数字组成,每个状态码都有特定的含义

PHP API开发的最佳实践方法PHP API开发的最佳实践方法Jun 17, 2023 pm 05:09 PM

随着互联网的发展,越来越多的应用程序需要对外暴露API接口,而PHP作为一种流行的服务器端脚本语言,自然不例外。然而,未经过良好设计和编写的API接口可能会导致安全性问题、性能瓶颈、功能不完整等问题。因此,在PHPAPI开发中,需要遵循一些最佳实践方法,以确保API的质量和可靠性。本文将介绍几个PHPAPI开发的最佳实践方法。1.好的URI设计URI(

PHP7.0中的多语言支持有哪些方法?PHP7.0中的多语言支持有哪些方法?May 26, 2023 am 08:31 AM

随着全球化的发展,多语言支持已经成为了越来越多网站开发者必须考虑的问题。在PHP7.0中,提供了多种方式来实现语言的本地化,以满足用户对于不同语言的需求。本文将介绍PHP7.0中的多语言支持方法及其优缺点。一、使用GettextGettext是PHP7.0内置的一个国际化工具包。它能够支持多种语言,并且可用于翻译所有用户界面的文本,包括

深入了解Linux ldconfig深入了解Linux ldconfigMar 14, 2024 pm 03:39 PM

Linuxldconfig是一个用于动态链接库管理的工具,可以帮助系统在运行时找到并加载共享库。它主要用于更新系统的动态链接器运行时连接库缓存,以保证程序可以正确链接到共享库。ldconfig主要用于两个方面:一是添加、删除共享库路径,并更新相关信息到配置文件中;二是根据配置文件中的路径重新生成动态连接库链接器的缓存。接下来将介绍如何使用ldconf

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)