728x90
SMALL

/////////

/////////

///////

// 키리시마 클래스 쿄코 출력

// オブジェクト生成時にフィルドを初期化しよう1

 

using System;

 

public class Practice

{

    public static void Main()

    {

        // オブジェクト生成時にコンストラクタに値を渡し、nameフィルドを初期化する

var kyo = new Human("霧島京子");

kyo.Greet();

    }

}

 

class Human

{

    private string name;

 

    public Human(string name)

    {

        this.name = name;

    }

 

    public void Greet()

    {

        Console.WriteLine("みなさんこんにちは" + name + "です");

    }

}

/////

///みなさんこんにちは霧島京子です

///

 

///

// RPGの敵クラスを作ろう

using System;

 

public class Lesson07

{

    public static void Main()

    {

Attack("슬라임");

 

    }

   

    public static void Attack(string enemy)

    {

        Console.WriteLine(enemy + "は勇者を攻した。");

    }

}

//// attack 슬라임

////

///

 

// RPGの敵クラスを作ろう

using System;

 

public class Lesson07

{

    public static void Main()

    {

//Attack("슬라임");

var enemy = new Enemy("몬스터");

enemy.Attack();

 

    }

   

   

}

public class Enemy

{

    private string name;

    public Enemy(string name)

    {

        this.name = name;

    }

     public void Attack()

    {

        Console.WriteLine(name + "は勇者を攻した。");

    }

   

}

///////////////

//////

 

// RPGの敵クラスを作ろう

using System;

 

public class Lesson07

{

    public static void Main()

    {

    Enemy[] enemies =

    {

        new Enemy("スライム"),

        new Enemy("モンスタ"),

        new Enemy("ドラゴン")

    };

   

foreach(var enemy in enemies)

{

enemy.Attack();

}

 

}

}

public class Enemy

{

    private string name;

    public Enemy(string name)

    {

        this.name = name;

    }

     public void Attack()

    {

        Console.WriteLine(name + "は勇者を攻した。");

    }

   

}

/////////////

///////영어의 instance의 의미에는 実例라는 의미가 있다.

728x90

설정

트랙백

댓글

728x90
SMALL

// 変数をクラスで管理しよう

using System;

 

public class Lesson07

{

    public static void Main()

    {

var player1 = new Player("전사");

player1.Walk();

 

 

    }

    public class Player

    {

        private string name;

        public Player(string name)

        {

            this.name = name;

        }

        public void Walk()

        {

            Console.WriteLine(name + "는 황야를 걸었다");

        }

       

    }

   

}

//

////////////

///////

// this없이

//////

// 変数をクラスで管理しよう

using System;

 

public class Lesson07

{

    public static void Main()

    {

var player1 = new Player("전사");

player1.Walk();

 

 

    }

    public class Player

    {

        private string name; //name을 대입한다. This가 없어서

// 필드, 클래스에 정의되어 있는 변수이다.

        public Player(string name)

        {

            name = name;

//constructor에서 사용하는 name을 그냥 대입해서 전사가 안 뜬다.

        }

        public void Walk()

        {

            Console.WriteLine(name + "는 황야를 걸었다");

        }

       

    }

   

}

////

///

// 変数をクラスで管理しよう

using System;

 

public class Lesson07

{

    public static void Main()

    {

var player1 = new Player("전사");

player1.Walk();

var player2 = new Player("魔法使い");

player2.Walk();

player1.Walk();

 

    }

    public class Player

    {

        private string name;

        public Player(string name)

        {

            this.name = name;

        }

        public void Walk()

        {

            Console.WriteLine(name + "는 황야를 걸었다");

        }

       

    }

   

}

 

728x90

설정

트랙백

댓글

728x90
SMALL

///////////

//////////

////////

7장 오브젝트: 변수와 메소드의 합

보통 오브젝트 지향의 프로그램이다.

클래스: 오브젝트의 설계도, 오브젝트: 클래스로부터 만들어짐

클래스(player) > 플레이어 오브젝트(paiza 공격, wizard 공격, dragon 공격)

오브젝트 = 인스턴스

1. 클래스를 작성한다.

2. 변수를 클래스에서 관리한다.

3. 구체적인 예: RPG의 적 클래스를 만든다.

4. 클래스에서, 인수와 인 메소드를 만든다.

5. 문자열이나 배열도 오브젝트로 되어 있다.

6. 억세스 수식자에 대해서 이해한다.

 

//

/////////

//

// クラスを作成しよう

using System;

 

public class Lesson07

{

    public static void Main()

    {

        var player1 = new Player(); //불러온다.

     Walk(); //static이 있는 Walk

     player1.Walk(); //출력

    }

   

    public static void Walk()

    {

        Console.WriteLine("勇者は荒野をいていた");

    }

}

public class Player

{

    public void Walk()

    {

        Console.WriteLine("勇者は");

    }

}

 

////

////

////

// クラスとメソッドを定義しよう

 

using System;

 

public class Practice

{

    public static void Main()

    {

        // Humanクラスのオブジェクトを生成してGreet()メソッドを呼び出す

var man = new Human(); //Human 클래스를 불러온다.

man.Greet(); //Greet의 명령어를 불러온다. 

    }

}

 

class Human

{

    public void Greet()

    {

        Console.WriteLine("こんにちは世界");

    }

}

/////

///

 

//////

// クラスとメソッドを定義しよう

 

using System;

 

public class Practice

{

    public static void Main()

    {

        var human = new Human();

        human.Greet();

    }

}

 

// Human クラスを定義する

class Human

{

    public void Greet()

    {

        Console.WriteLine("さようなら世界");

    }

   

}

///

 

728x90

설정

트랙백

댓글

728x90
SMALL

////

// 長引

using System;

 

public class Lesson06

{

    public static void Main()

    {

// 얼마나 히키수가 들어가는 지 모를때

Introduce("霧島");

Introduce("勇者","","魔法使い");

//長引 params를 사용한다.

    }

   

    public static void Introduce(params string[] people)

    {

        foreach(string name in people)

        {

            Console.WriteLine("私は" + name + "です");

        }

    }

   

}

///////

私は霧島です
私は勇者です
私は戦士です
私は魔法使いです

///

// 長引

using System;

 

public class Program

{

    public static void Main()

    {

        Introduce("スライム", "ドラゴン", "魔王");

    }

 

    public static void Introduce(params string[] enemies)

    {

        foreach (var enemy in enemies)

        {

            Console.WriteLine("士は" + enemy + "った");

        }

    }

}

//////////

//

戦士はスライムと戦った
戦士はドラゴンと戦った
戦士は魔王と戦った

728x90

설정

트랙백

댓글

728x90
SMALL

// 名前付き引を理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

 

SayHello(target: "네코센세");

SayHello(greeting:"おはようございます。");

// 생략 시킬 수도 있다.

    }

   

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

       

        Console.WriteLine(greeting + " " + target);

    }

}

///////

///

///

// 名前付き引を理解しよう

using System;

 

public class Program

{

    public static void Main()

    {

        SayHello(target: "paiza");

    }

 

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

        Console.WriteLine(greeting + " " + target);

    }

}

///

///

///

///

 

728x90

설정

트랙백

댓글

728x90
SMALL

//이름을 붙인 引数

//引数를 생략하고 싶을 때 붙이는 디폴트치

//

// 名前付き引を理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

SayHello(); //hello world가 대입된다.

SayHello("こんにちは","皆さん"); // 디폴트치 대신에 입력한 것이 들어간다.

//곤니치와 미나상

    }

   

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

       

        Console.WriteLine(greeting + " " + target);

    }

}

 

// 名前付き引を理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

SayHello(); //hello world가 대입된다.

SayHello("こんにちは","皆さん"); // 디폴트치 대신에 입력한 것이 들어간다.

SayHello("Good morning!"); // Good morning world가 출력된다.

// 이 경우는 두 번째 변수가 생략되어 world가나온다

 

 

    }

   

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

       

        Console.WriteLine(greeting + " " + target);

    }

   

   

}

///

///

// 名前付き引を理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

SayHello(); //hello world가 대입된다.

SayHello("こんにちは","皆さん"); // 디폴트치 대신에 입력한 것이 들어간다.

SayHello("Good morning!"); // Good morning world가 출력된다.

// 이 경우는 두 번째 변수가 생략되어 world가나온다

SayHello(greeting: "こんにちは", target: "皆さん");

SayHello(target: "ネコ先生",greeting:"おはようございます。");

// 순번을 바꾸는 것이 가능하다.

 

    }

   

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

       

        Console.WriteLine(greeting + " " + target);

    }

   

   

}

///

//////////

728x90

설정

트랙백

댓글

728x90
SMALL

// 디폴트

/////////////////

// のデフォルト値

using System;

 

public class Lesson06

{

    public static void Main()

    {

    Introduce("勇者");

    }

   

    public static void Introduce(string name)

    {

        Console.WriteLine("私は" + name + "です。");

    }

   

}

////

//私は勇者です。

 

//

//

// のデフォルト値

using System;

 

public class Lesson06

{

    public static void Main()

    {

    Introduce("勇者");

    Introduce();

    }

   

    public static void Introduce(string name = "村人")

    {

        Console.WriteLine("私は" + name + "です。");

    }

   

}

//Introduce에서

// 디폴트치를 지정할 수 있다.

// 私は勇者です。
// 私は村人です

//

//

// のデフォルト値

using System;

 

public class Program

{

    public static void Main()

    {

SayHello("Hello");

    }

 

    public static void SayHello(string greeting, string target = "paiza")

    {

        Console.WriteLine(greeting + " " + target);

    }

}

//////

//

728x90

설정

트랙백

댓글

728x90
SMALL

//RPGの攻ンを作ろう

using System;

 

public class Program

{

    public static void Main()

    {

        string[] players = { "勇者", "", "魔法使い" };

        var rand = new Random();

        int enemyHp = int.Parse(Console.ReadLine());

 

        foreach (var player in players)

        {

            var hit = rand.Next(1, 4) * 10;

            Console.WriteLine(player + "はスライムを攻した");

            // 下記にコドを追加する

            enemyHp = enemyHp-hit;

            Console.WriteLine("敵のHP" + enemyHp + "です");

        }

    }

 

    public static int Attack(int enemyHp, int hit)

    {

        enemyHp -= hit;

        return enemyHp;

    }

}

 

//

///

 

// RPGの攻ンを作ろう

using System;

 

public class Lesson06

{

    public static void Main()

    {

 

Console.WriteLine("Hello World");

// 변수의 스코프

var num = 0;

if (num == 0)

{

    var message = "paiza";

    Console.WriteLine(message + " " + num);

   

}

// 블록 밖에서는 사용 불가능

// Console.WriteLine(message + " " + num);//error

 

    }

   

   /* public static ()

    {

       

    } */

   

}

/////

///

// RPGの攻ンを作ろう

using System;

 

public class Lesson06

{

    public static void Main()

    {

Console.WriteLine("Hello World");

// 변수의 스코프

var num = 0;

if (num == 0)

{

    var message = "paiza";

    Console.WriteLine(message + " " + num);

   

}

// 블록 밖에서는 사용 불가능

// Console.WriteLine(message + " " + num);//error

//message가 없음

 

for(var i = 1; i<5;i++)

{

    var message ="C#";

    Console.WriteLine(message + " " + i);

}

 

// Console.WriteLine(message);

//에러가 뜬다.

    }

 

}

/////////

// 블록 안에 설치된 변수만 실행이 된다.

// 변수의 스코프를 인식할 필요가 있다.

728x90

'프로그래밍' 카테고리의 다른 글

C# paiza 54 - 이름을 붙인 引数  (0) 2020.05.16
C# paiza 53 - 인수의 디폴트치1  (0) 2020.05.16
C# paiza 51 - RPG의 공격 Scene1  (0) 2020.05.16
C# paiza 50 - 스코프  (0) 2020.05.16
C# paiza 49 - 곱셉 메소드 만들기  (0) 2020.05.16

설정

트랙백

댓글