[닷넷C#, 구로가산C#윈폼교육]추상 속성사용예(abstract Property), override, 오라클자바커뮤니티 닷넷C#강좌
C#의 속성도 abstract를 붙일 수 있습니다. 하위클래스에서는 override를 해야 하구요, 아래 예제를 참고 하세요.
abstract class Shape
{
public abstract double Area
{
get;
set;
}
}
class Square : Shape
{
public double side;
public Square(double s) //constructor
{
side = s;
}
public override double Area
{
get
{
return side * side;
}
set
{
side = System.Math.Sqrt(value);
}
}
}
class Cube : Shape
{
public double side;
public Cube(double s)
{
side = s;
}
public override double Area
{
get
{
return 6 * side * side;
}
set
{
side = System.Math.Sqrt(value / 6);
}
}
}
class TestShapes
{
static void Main()
{
// Input the side:
System.Console.Write("Enter the side: ");
double side = double.Parse(System.Console.ReadLine());
// Compute the areas:
Square s = new Square(side);
Cube c = new Cube(side);
// Display the results:
System.Console.WriteLine("Area of the square = {0:F2}", s.Area);
System.Console.WriteLine("Area of the cube = {0:F2}", c.Area);
System.Console.WriteLine();
// Input the area:
System.Console.Write("Enter the area: ");
double area = double.Parse(System.Console.ReadLine());
// Compute the sides:
s.Area = area;
c.Area = area;
// Display the results:
System.Console.WriteLine("Side of the square = {0:F2}", s.side);
System.Console.WriteLine("Side of the cube = {0:F2}", c.side);
}
}
[결과]
Enter the side: 3
Area of the square = 9.00
Area of the cube = 54.00
Enter the area: 4
Side of the square = 2.00
Side of the cube = 0.82
평일주간[100%환급과정]
(8/29)SQL기초에서 Schema Object까지(8/29)Spring,MyBatis,Hibernate실무과정
(8/29)자바기초JDBC,Servlet/JSP까지
(8/29)PL/SQL,ORACLE HINT,TUNING
(9/15)C#4.0,WinForm,ADO.NET
(9/15)안드로이드개발자과정
(9/18)오라클자바채용확정교육
평일야간[개인80%환급]
(8/28)SQL기초에서실무까지
(8/28)자바JSP,jQuery,Spring,MyBatis
(8/29)Spring, MyBatis, Hibernate
(9/02)HTML5,CSS3,Ajax,jQuery마스터
(9/12)C#,Network,ADO.NET,ASP.NET
(9/16)안드로이드개발자과정
주말주간[개인80%환급]
(8/30)Spring, MyBatis, Hibernate
(8/30)SQL기초에서실무까지
(8/30)자바,네트워크,웹&스프링
(8/30)안드로이드개발자과정
(9/13)C#,ASP.NET마스터(9/13)웹퍼블리싱 마스터
댓글 없음:
댓글 쓰기