본문 바로가기
유니티

6/28 C#

by SUGI_ 2023. 6. 28.
  • 다른 객체의 메소드를 호출하여 실행하는 방법
namespace ConsoleApp7
{
    class Calc
    {
        //인자값도 넣을 수 있고 반환값도 있는 함수
        public int add(int a , int b)
        {
            return a + b;
        }

        public void print()
        {
            int c;
            c = add(8, 6);
            Console.WriteLine(c);
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Calc calc = new Calc(); //클래스 객체 생성
            calc.print();
            Console.WriteLine("3 + 9 = " + calc.add(3, 9));
        }
    }
}
namespace ConsoleRef
{
    // out 반환값외의 인수를 사용해서 복수의 값을 반환할 수 있다.
    class Cale
    {
        public void div(int a, int b, out int q, out int r)
        {
            q = a / b;
            r = a % b;
        }

        public void print()
        {
            int x, y;
            div(10, 3, out x, out y);
            Console.WriteLine("x : " + x + " y : " + y);
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            int x, y;

            Cale cale = new Cale();

            cale.print();
        }
    }
}
namespace ConsoleRef
{
    class Cale
    {
        static void Add10(ref int a)
        {
            a = a + 10;
        }

        public void print()
        {
            int x = 100; //x 100이다.
            Add10(ref x); //c / c++ 포인터 ref 주소값을 넘겨준다.       
            Console.WriteLine(x);
        }
    }

    internal class Class1
    {
        static void Main(string[] args)
        {
            Cale cale = new Cale();
            cale.print();
        }
    }
}

 

  • 오버로드 Overload

메소드이름이 같을 수 있게 만들어 줌.

인수의 숫자가 다르거나 자료형이 서로 다를 때

메소드 이름이 같을 수 있다.

namespace Overload
{
    class Cat
    {
        string name;
        string place;
        int age;

        public void SetData(string n, string p, int a)
        {
            name = n;
            place = p;
            age = a;
        }

        //오버로딩
        public void print()
        {
            Console.WriteLine(place + ":" + name + ":" + age + "세");
        }

        public void print(string p, int a)
        {
            place = p;
            Console.WriteLine(place + ": 고양이는" + a + "마리입니다.");
        }

        public void print(string variety)
        {
            Console.WriteLine(place + ":" + name + "" + age + "세" + variety);
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Cat cat1 = new Cat();
            Cat cat2 = new Cat();
            Cat cat3 = new Cat();

            cat1.SetData("로빈", "우리집", 10);
            cat2.SetData("꼬마", "옆집", 14);
            cat1.print("잡종");
            cat2.print();
            cat3.print("뒷집", 0);
        }
    }
}

namespace Overload
{
    class Calc
    {
        //public double Calccircle(int rad, double pi)
        //public double Calccircle(int rad = 1, double pi)
        public double Calccircle(int rad = 1, double pi = 3.14)
        {
            return rad * rad * pi;
        }
    }

    internal class Class1
    {
        static void Main(string[] args)
        {
            Calc cale = new Calc();
            //선택적인수 호출
            Console.WriteLine(cale.Calccircle());
            Console.WriteLine(cale.Calccircle(10));
            // 인자값을 원하느 부분에 넣을 수 있음
            Console.WriteLine(cale.Calccircle(pi:20,rad:3));
            Console.WriteLine(cale.Calccircle(20,3));
        }
    }
}

오버라이딩 차이점 알아보기

https://ssabi.tistory.com/49

 

[C#] 오버로딩(Overloading), 오버라이딩(Overriding)

오버로딩(Overloading) 오버로딩의 사전적 의미는 과적하다 입니다. 오버로딩은 하나의 메소드에 여러 가지로 구현하는 것을 말합니다. 오버로딩을 하게 되면 하나의 메소드에 여러 개의 구현을 과

ssabi.tistory.com

 

  • Static

객체를 생성하지 않고 직접 

https://luv-n-interest.tistory.com/774

 

유니티에서 Static이란 [Unity]

static.. Dynamic의 반대로 알고 있다. 정적을 의미한다. 즉, 런타임 이전에 훨씬 이전에 수행되어 만들어진다는 뜻으로 알고 있다. 사실 선언했을 때 만들어진다. ** 메모리에서의 Data에 저장된다. 그

luv-n-interest.tistory.com

 

public 어디서든 접근가능하게

private 다른 객체에서 엑세스 불가

protected 클래스에 상속일때

internal 어셈블리 단위에서 엑세스

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 접근제어자
{
     class Cat
    {
        private string a ;//Shiro
        string b ;//Tama
        public string c = "Tora";

        public void SetA(string _a)
        {
            a = _a;
        }

        public void SetB(string _b)
        {
            b = _b;
        }

        public string GetA()
        {
            return a;
        }

        public string GetB()
        {
            return b;
        }


    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Cat name = new Cat();

            name.SetA("Shiro");
            name.SetB("Tama");

            Console.WriteLine(name.GetA());
            Console.WriteLine(name.GetB());
            Console.WriteLine(name.c);

            //문제
            //위의 private 오류를 해결하세요. 
            //힌트 함수를 이용해서 a는 Shiro 
            //     B는 Tama 데이터를 넣고
            //출력하시오.
        }
    }
}
namespace ConsoleApp8
{
    class Book
    {
        public int price;
        public int num = 0;
        public string title;

        public Book(string t, int p) //인자있는 생성자
        {
            title = t;
            price = p;
        }

        public void print()
        {
            Console.WriteLine("제   목" + title);
            Console.WriteLine("정   가" + price);
            Console.WriteLine("주문 부수:" + num);
            Console.WriteLine("합계 금액:" + price * num);
        }
    }

    internal class Class2
    {
        static void Main(string[] args)
        {
            // 생성자에 인자를 넣을 수 있음
            Book book = new Book("C 그림책", 140000);
            book.num = 10;
            book.print();
        }
    }
}
728x90

'유니티' 카테고리의 다른 글

6/28 특정오브젝트 찾기, 인스턴스화  (0) 2023.06.28
6/21 배열 , 형변환  (0) 2023.06.28
유니티 화면녹화  (0) 2023.06.27
유니티 입사각 반사각  (0) 2023.06.27
비주얼 스튜디오 단축키  (0) 2023.06.27