Development/C#

[프랙티컬 C# 연습문제] 1부 - 1장

이쥬딩 2023. 7. 12. 18:51
728x90

문제 1.1

using changename;

namespace helloworld
{
  class Program
  {
    static void Main(string[] args)
    {
      Product pullbread = new Product(95, "풀빵", 210);
      Console.WriteLine(pullbread.GetPriceIncludingTax());
    }
  }
}
namespace changename
{
    public class Product
    {
        public int code { get; private set; }
        public string Name { get; private set; }
        public int price { get; private set; }

        public Product(int code, string name, int price)
        {
            this.code = code;
            this.Name = name;
            this.price = price;
        }

        public int GetTax()
        {
            return (int)(price * 0.08);
        }

        public int GetPriceIncludingTax()
        {
            return price + GetTax();
        }
    }
}

문제 1.2

namespace changename
{
    class MyClass
    {
        public int X { get; set; }
        public int Y { get; set; }

        public MyClass(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
    }

    struct MyStruct
    {
        public int X { get; set; }
        public int Y { get; set; }

        public MyStruct(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
    }

}
using System;
using changename;

namespace helloworld
{
  class Program
  {
    static void PrintObjects(MyClass a, MyStruct b)
    {
      Console.WriteLine(String.Format("{0},{1}", a.X, a.Y));
      Console.WriteLine(String.Format("{0},{1}", b.X, b.Y));

      a.X *= 2;
      a.Y *= 2;
      b.X *= 2;
      b.Y *= 2;
    }

    static void Main(string[] args)
    {
      MyClass a = new MyClass(10, 20);
      MyStruct b = new MyStruct(10, 20);
      PrintObjects(a, b);
      Console.WriteLine(String.Format("{0},{1}", a.X, a.Y));
      Console.WriteLine(String.Format("{0},{1}", b.X, b.Y));
    }
  }
}

[결과]

10,20
10,20
20,40
10,20

[이유]

MyClass는 참조형이므로 참조형 객체를 인수로 지정하면 객체를 가리키는 참조가 복사되어 메서드에 전달된다. 따라서 객체의 값을 메서드 안에서 변경하면 호출한 쪽의 객체가 그 참조를 통해 변경된다.

MYStruct는 값형이므로 값 자체가 복사되어 메서드에 전달되므로 메서드 안에서 객체의 값을 변경해도 호출한 쪽에서는 영향을 받지 않는다.

 

문제 1.3

using System;

namespace helloworld
{
  public class Person
  {
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
    public int GetAge()
    {
      DateTime today = DateTime.Today;
      int age = today.Year - Birthday.Year;
      if (today < Birthday.AddYears(age)) age--;
      return age;

    }
  }

  public class Student : Person
  {
    public int Grade { get; set; }
    public int ClassNumber { get; set; }

    public Student(string name, DateTime birthday, int grade, int classNumder)
    {
      this.Name = name;
      this.Birthday = birthday;
      this.Grade = grade;
      this.ClassNumber = classNumder;
    }
  }
  class Program
  {
    static void Main(string[] args)
    {
      Student student = new Student("Judy", new DateTime(1992, 4, 5), 3, 5);

      Console.WriteLine("{0},{1},{2},{3}", student.Name, student.Birthday, student.Grade, student.ClassNumber);

      Person copy_person = student;
      object copy_object = student;
    }
  }
}

서브 클래스(파생 클래스)의 인스턴스는 슈퍼클래스(기저클래스) 타입 형 변수에 대입할 수 있다. 

하지만, 반대는 안 된다.

728x90