mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-05 06:15:24 +00:00
Initial commit
This commit is contained in:
parent
bc96ccb183
commit
18f89e91d4
62 changed files with 1661 additions and 2 deletions
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
29
vscode/Chapter06/Ch06Ex02Inheritance/Circle.cs
Normal file
29
vscode/Chapter06/Ch06Ex02Inheritance/Circle.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class Circle : Square
|
||||
{
|
||||
public Circle() { }
|
||||
|
||||
public Circle(double radius) : base(width: radius * 2) { }
|
||||
|
||||
public double Radius
|
||||
{
|
||||
get
|
||||
{
|
||||
return height / 2;
|
||||
}
|
||||
set
|
||||
{
|
||||
Height = value * 2;
|
||||
}
|
||||
}
|
||||
|
||||
public override double Area
|
||||
{
|
||||
get
|
||||
{
|
||||
double radius = height / 2;
|
||||
return Math.PI * radius * radius;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
vscode/Chapter06/Ch06Ex02Inheritance/Program.cs
Normal file
10
vscode/Chapter06/Ch06Ex02Inheritance/Program.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using Packt.Shared;
|
||||
|
||||
Rectangle r = new(height: 3, width: 4.5);
|
||||
WriteLine($"Rectangle H: {r.Height}, W: {r.Width}, Area: {r.Area}");
|
||||
|
||||
Square s = new(5);
|
||||
WriteLine($"Square H: {s.Height}, W: {s.Width}, Area: {s.Area}");
|
||||
|
||||
Circle c = new(radius: 2.5);
|
||||
WriteLine($"Circle H: {c.Height}, W: {c.Width}, Area: {c.Area}");
|
||||
20
vscode/Chapter06/Ch06Ex02Inheritance/Rectangle.cs
Normal file
20
vscode/Chapter06/Ch06Ex02Inheritance/Rectangle.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class Rectangle : Shape
|
||||
{
|
||||
public Rectangle() { }
|
||||
|
||||
public Rectangle(double height, double width)
|
||||
{
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public override double Area
|
||||
{
|
||||
get
|
||||
{
|
||||
return height * width;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
vscode/Chapter06/Ch06Ex02Inheritance/Shape.cs
Normal file
37
vscode/Chapter06/Ch06Ex02Inheritance/Shape.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public abstract class Shape
|
||||
{
|
||||
// fields
|
||||
protected double height;
|
||||
protected double width;
|
||||
|
||||
// properties
|
||||
public virtual double Height
|
||||
{
|
||||
get
|
||||
{
|
||||
return height;
|
||||
}
|
||||
set
|
||||
{
|
||||
height = value;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual double Width
|
||||
{
|
||||
get
|
||||
{
|
||||
return width;
|
||||
}
|
||||
set
|
||||
{
|
||||
width = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Area must be implemented by derived classes
|
||||
// as a read-only property
|
||||
public abstract double Area { get; }
|
||||
}
|
||||
26
vscode/Chapter06/Ch06Ex02Inheritance/Square.cs
Normal file
26
vscode/Chapter06/Ch06Ex02Inheritance/Square.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class Square : Rectangle
|
||||
{
|
||||
public Square() { }
|
||||
|
||||
public Square(double width) : base(height: width, width: width) { }
|
||||
|
||||
public override double Height
|
||||
{
|
||||
set
|
||||
{
|
||||
height = value;
|
||||
width = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override double Width
|
||||
{
|
||||
set
|
||||
{
|
||||
height = value;
|
||||
width = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue