Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ クラスメソッド | オブジェクト指向プログラミング(OOP)イントロダクション
C#オブジェクト指向構造

bookクラスメソッド

メニューを表示するにはスワイプしてください

構造体と同様に、クラスにもメソッドを持たせることができます。メソッドの作成や呼び出しの構文も非常に似ています。

例えば、Rectanglewidth 属性を持つ height クラスと、areawidth の値に基づいて長方形オブジェクトの面積を計算する height メソッドを作成できます。

index.cs

index.cs

copy
1234567
class className { // ... other class code public returnType methodName(datatype param1, datatype param2, ...) { // code } }
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; public class ConsoleApp { class Rectangle { public double width; public double height; public double area() { return width * height; } } public static void Main(string[] args) { Rectangle r1 = new Rectangle(); r1.width = 10; r1.height = 20; Rectangle r2 = new Rectangle(); r2.width = 14.7; r2.height= 17.9; Console.WriteLine($"Area of R1 is {r1.area()}"); Console.WriteLine($"Area of R2 is {r2.area()}"); } }
question mark

次のうち、有効なメソッドはどれですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  7
some-alt