-
-
Save tuannguyenssu/d5749ed0cf0d5be4783a81180fe8d933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Rectangle | |
{ | |
public int Height { get; set; } | |
public int Width { get; set; } | |
public virtual void SetHeight(int height) | |
{ | |
this.Height = height; | |
} | |
public virtual void SetWidth(int width) | |
{ | |
this.Width = width; | |
} | |
public virtual void PrintArea() | |
{ | |
Console.WriteLine(this.Height * this.Width); | |
} | |
} | |
public class Square : Rectangle | |
{ | |
public override void SetHeight(int height) | |
{ | |
this.Height = height; | |
this.Width = height; | |
} | |
public override void SetWidth(int width) | |
{ | |
this.Height = width; | |
this.Width = width; | |
} | |
} | |
Rectangle rect = new Rectangle(); | |
rect.SetHeight(10); | |
rect.SetWidth(5); | |
rect.PrintArea(); // Kết quả là 5 * 10 | |
// Below instantiation can be returned by some factory method | |
Rectangle rect1 = new Square(); | |
rect1.SetHeight(10); | |
rect1.SetWidth(5); | |
rect1.PrintArea(); | |
// Kết quả là 5 x 5. Nếu đúng phải là 10x5, vì diện tích 1 hình chữ nhật là dài x rộng | |
// Class Square sửa hành vi của class cha Rectangle, set cả dài và rộng về 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment