【C#】关于Method()方法的学习理解(下)

上半部分详细的概念介绍请见上一篇帖子【C#】关于Method方法的学习理解(上)link如下:
https://blog.csdn.net/weixin_44174312/article/details/142170824

Placing a Method in a Class
将方法放入类中

方法出现的顺序不重要!方法调用的顺序才重要!!!
The order in which the methods appear doesn’t matter! It’s the order of method calls that matters!!!

By convention, programmers indent the statements in a method body, makes the method header and its braces stand out
• In Visual Studio editor, the method statements are indented for you automatically
• Methods cannot overlap
– Usually you want to place methods one after the other within a class
– The order that methods appear in a class is not important
– It is only the order in which they are called that affects how they execute

按照惯例,程序员缩进方法体中的语句,使方法头及其大括号突出
•在Visual Studio编辑器中,方法语句会自动缩进
•方法不能重叠
-通常你想在一个类中一个接一个地放置方法
方法在类中出现的顺序并不重要
-只有调用的顺序才会影响它们的执行方式

Local function
– When a method resides entirely within another method
– It can be called only from its containing method and not from any other methods
在这里插入图片描述
在这里插入图片描述


Declaring Variables and Constants in a Method

在方法中声明变量和常量

Variables and constants can be declared in a method
– In scope only from the point at which they are declared to the end of the method
• Scope
– The area in which a variable is known
• Programmers say that the area in which an item can be used is the area in which it is visible
• They are considered local to that method
– Their scope is defined by the boundaries of the method
• They are not accessible outside of the method except when
passed as parameters to other methods

变量和常量可以在方法中声明
-仅在范围内从声明它们的点到方法结束
•范围
—变量已知的区域
•程序员说一个项目可以被使用的区域,它所在的区域是否可见
•它们被认为是该方法的局部
-它们的作用域由方法的边界定义
•它们不能在方法外部访问,除非作为参数传递给其他方法在这里插入图片描述

在这里插入图片描述


Writing Methods with No Parametersand No Return Value
编写无参数、无返回值的方法
在这里插入图片描述

Writing Methods That Require a Single Argument

编写只需要一个参数的方法

When you write the declaration for a method that accepts a parameter, you need to include the following items within the
method declaration parentheses:
– The type of the parameter
– A local identifier (name) for the parameter
• Local variable
– Declared within a method
• Formal parameter
– A parameter in a method header that accepts a value
• Actual parameters
– Arguments within a method call

为接受参数的方法编写声明时,需要将以下项包含在
方法声明括号:
-参数类型
—参数的本地标识(名称)
•局部变量
-在方法中声明
•形式参数
-方法头中接受值的参数
•实际参数
-方法调用中的参数
在这里插入图片描述

在这里插入图片描述

Choose True or False
• When you write the declaration for a method that accepts a
parameter,

    1. you need to include the parameter’s data type within the method header. True
    1. you need to include the identifier of the argument that will be sent to the
      method within the method header. Flase

False: You do not need to include the identifier of the argument that will be sent to the method within the method header (only its type is required in the method declaration; the actual argument identifier is provided when the method is called).
False:你不需要在方法头文件中包含将被发送给方法的参数的标识符(在方法声明中只需要它的类型;实际的参数标识符在调用方法时提供)。

    1. you need to include a local identifier for the parameter within the method
      header.
      True (with clarification): You need to include a local identifier for the parameter within the method header. This identifier is used within the method to refer to the value passed as an argument when the method is called.
      True(需要澄清):您需要在方法头中包含参数的本地标识符。此标识符在方法中用于引用在调用方法时作为参数传递的值。

Writing Methods That Require Multiple Arguments (1 of 2) 编写需要多个参数的方法(1 / 2)

Methods can take any number of parameters in any order
• When you call the method, arguments must match (in number and type) the parameters listed in the method declaration, with the following exceptions:
– The type of an argument does not need to match the parameter list exactly if the argument can be promoted to the parameter type; int argument can be passed to a double parameter
方法可以以任意顺序接受任意数量的参数
•当你调用方法时,参数必须(在数量和类型上)与方法声明中列出的参数匹配,但有以下例外:
-参数的类型不需要与参数列表完全匹配,如果参数可以提升为参数类型;整型实参可以传递给双形参

在这里插入图片描述

Writing Methods That Return a Value 编写返回值的方法

A method can return, at most, one value to a method that calls it
• Method’s type
– A method’s return type
• return statement
– Causes a value to be sent back to the calling method
• You are not required to use a returned value
一个方法最多只能向调用它的方法返回一个值
•方法的类型
-方法的返回类型
•返回语句
—将值返回给调用方法
•您不需要使用返回值

Suppose you want to create a method to accept the hours
an employee worked and the hourly pay rate, and to return
a calculated gross pay.
在这里插入图片描述

Writing Methods That Return a Value

编写返回值的方法

• A returned value can be:
– Stored in a variable
– Used directly
• Nested method calls
– Method calls placed inside other method calls
• Writing a method that returns a Boolean value
– When a method returns a value that is type bool, the
method call can be used anywhere you can use a Boolean expression
Branching statements (e.g., if(…))
Loop control variables

•返回值可以是:
—存储在变量中
-直接使用
•嵌套方法调用
-将方法调用置于其他方法调用内部
•编写一个返回布尔值的方法
—当方法返回bool类型的值时
方法调用可以用在任何可以使用布尔表达式的地方
分支语句(例如,if(…))
循环控制变量

在这里插入图片描述
在这里插入图片描述
Choose True or False
• 1. A method can return, at most, one value to a method that calls it.
一个方法最多只能向调用它的方法返回一个值。 True
• 2. The data type used in a method’s return statement must be the same as
the return type declared in the method’s header.
方法返回语句中使用的数据类型必须与在方法头文件中声明的返回类型 True

• 3. If a method returns a value and you call the method, you must store the
value in a variable that has the same data type as the method’s parameter. False
如果一个方法返回一个值,并且您调用该方法,则必须存储与方法的参数具有相同数据类型的变量中的值。 False
If a method returns a value and you call the method, you must store the value in a variable that has the same data type as the method’s return type, not the method’s parameter. The data type of the variable that stores the returned value must match the return type of the method, not necessarily the type of any parameters the method may accept.
如果一个方法返回一个值,而您调用了该方法,则必须将该值存储在与该方法的返回类型具有相同数据类型的变量中,而不是存储在该方法的参数中。存储返回值的变量的数据类型必须与方法的返回类型匹配,而不一定与方法可以接受的任何参数的类型匹配。

Writing a Method That Returnsa Boolean Value

编写一个返回布尔值的方法

Methods can return a value that is type bool
– Makes for more readable if statements
• e.g. A method named isPreferredCustomer() that returns a
Boolean value indicating whether a customer is a preferred
customer who qualifies for a discount
– If statement can be written as:
if (isPreferredCustomer())
price = price − DISCOUNT;
• When using Boolean methods, you must be careful not to cause
unintended side effects

方法可以返回bool类型的值
-使if语句更具可读性
•例如,一个名为isPreferredCustomer()的方法返回一个
指示客户是否为首选的布尔值
有资格享受折扣的顾客

  • If语句可以写成:
    is(isPreferredCustomer ())
    price = price−DISCOUNT;
    •当使用布尔方法时,必须注意不要引起意想不到的副作用

Analyzing a Built-In Method

C# provides many pre-written methods
– e.g. Pow()- raises a number to a power double result = Math.Pow(2.0, 3.0);
• From this statement, you know the following about the
Pow() method:
– It is in the Math class because the class name and a dot precede
the method call
– It is public because you can write a program that uses it
– It is static because it is used with its class name and a dot,without any object
– It accepts two double parameters
– It returns a double (or a type that can be promoted automatically
to a double (such as an int) because its answer is stored in a double)
c#提供了许多预先编写的方法
-例如Pow()-将一个数字提升为幂双精度结果= Math.Pow(2.0, 3.0);
•从这句话中,你可以知道关于
Pow()方法:
-它在数学类中,因为类名和前面的点
方法调用
-它是公共的,因为你可以编写一个程序来使用它
-它是静态的,因为它与类名和点一起使用,没有任何对象
-接受两个双参数
-返回双精度类型(或可自动提升的类型)
到双精度型(比如int型),因为它的答案存储在双精度型中

Chapter Summary
• A method is a series of statements that perform a task
• Object-oriented programs hide their methods’ implementation
• You write methods to make programs easier to understand, and so that you can easily reuse them
• Some methods require passed-in information called arguments or parameters
• You can pass multiple arguments to a method
• The return type for a method can be any type used in the C# programming language
•方法是执行任务的一系列语句
•面向对象程序隐藏了它们的方法实现
编写方法是为了让程序更容易理解,这样就可以很容易地重用它们
•一些方法需要传入称为参数或参数的信息
•你可以向一个方法传递多个参数
•方法的返回类型可以是c#编程语言中使用的任何类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值