C# 通过其他实例变量集方法更新实例变量
我的家庭作业有问题。我有一个实例变量price,它应该有一个get方法和一个no set方法。我通过构造函数计算并分配了这个价格变量。我注意到,如果将宽度或高度值更改为C# 通过其他实例变量集方法更新实例变量,c#,oop,instance-variables,C#,Oop,Instance Variables,我的家庭作业有问题。我有一个实例变量price,它应该有一个get方法和一个no set方法。我通过构造函数计算并分配了这个价格变量。我注意到,如果将宽度或高度值更改为Main中的另一个数字,则价格变量不会更改。我添加了一个updatePrice()方法,如果在Main中显式调用,该方法将起作用,但希望通过height/width set方法自动实现。我现在不能让它工作 更改有效的宽度变量后,显式调用updatePrice() using System; using static System.
Main
中的另一个数字,则价格变量不会更改。我添加了一个updatePrice()
方法,如果在Main
中显式调用,该方法将起作用,但希望通过height/width set方法自动实现。我现在不能让它工作
更改有效的宽度变量后,显式调用updatePrice()
using System;
using static System.Console;
class Photo
{
private int width { get; set; }
private int height { get; set; }
protected double price;
public int Width {
get
{
return width;
}
set
{
updatePrice(value, height);
width = value;
}
}
public int Height {
get
{
return height;
}
set
{
updatePrice(width, value);
height = value;
}
}
public Photo(int width, int height)
{
this.width = width;
this.height = height;
if (width == 8 && height == 10)
price = 3.99;
else if (width == 10 && height == 12)
price = 5.99;
else
price = 9.99;
}
public virtual double Price
{
get
{
return price;
}
}
public String ToString()
{
return GetType() + " with a width of " + width + " and a height of " + height +
" with a base price of " + Price.ToString("C2");
}
// used to be updatePrice() w/ no parameters
public void updatePrice(int width, int height)
{
if (width == 8 && height == 10)
price = 3.99;
else if (width == 10 && height == 12)
price = 5.99;
else
price = 9.99;
}
static void Main()
{
Photo photo = new Photo(10, 12);
WriteLine(photo.ToString());
photo.height = 4;
// updatePrice();
WriteLine(photo.ToString());
}
}
照片宽度为10,高度为12,底价为5.99美元
//将高度更改为4
照片宽度为10,高度为4,底价为5.99美元
//价格应为$9.99而不是更新价格,在
价格
属性中动态计算价格。像这样,您肯定它总是反映当前状态
public virtual double Price
{
get
{
if (width == 8 && height == 10) return 3.99;
if (width == 10 && height == 12) return 5.99;
return 9.99;
}
}
我刚刚用上述属性进行了速度测试:
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 1_000_000; i++) {
photo.Height = 4;
double price = photo.Price;
photo.Height = 10;
price = photo.Price;
}
stopWatch.Stop();
Console.WriteLine("Elapsed ms: " + stopWatch.ElapsedMilliseconds);
然后你就可以打印照片了
WriteLine(photo);
as
WriteLine
现在自动使用此方法。我还使用了字符串插值。这比字符串串联更具可读性。在price
属性中动态计算价格,而不是更新价格。像这样,您肯定它总是反映当前状态
public virtual double Price
{
get
{
if (width == 8 && height == 10) return 3.99;
if (width == 10 && height == 12) return 5.99;
return 9.99;
}
}
我刚刚用上述属性进行了速度测试:
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 1_000_000; i++) {
photo.Height = 4;
double price = photo.Price;
photo.Height = 10;
price = photo.Price;
}
stopWatch.Stop();
Console.WriteLine("Elapsed ms: " + stopWatch.ElapsedMilliseconds);
然后你就可以打印照片了
WriteLine(photo);
as
WriteLine
现在自动使用此方法。我还使用了字符串插值。这比字符串连接更具可读性。这是我在对问题的评论中建议的代码重构,将updatePrice
标记为protectedvirtual以允许多态性,在ToString
中添加了.Name
,我删除了私有字段的无用访问器:
class Photo
{
private int width;
private int height;
protected double price;
public int Width {
get
{
return width;
}
set
{
width = value;
updatePrice();
}
}
public int Height {
get
{
return height;
}
set
{
height = value;
updatePrice();
}
}
public Photo(int width, int height)
{
this.width = width;
this.height = height;
updatePrice();
}
public virtual double Price
{
get
{
return price;
}
}
public override string ToString()
{
return GetType().Name +
" with a width of " + width +
" and a height of " + height +
" with a base price of " + Price.ToString("C2");
}
protected virtual void updatePrice()
{
if (width == 8 && height == 10)
price = 3.99;
else if (width == 10 && height == 12)
price = 5.99;
else
price = 9.99;
}
}
因此,您不需要从外部调用updatePrice:
static void Main()
{
Photo photo = new Photo(10, 12);
WriteLine(photo.ToString());
photo.height = 4;
WriteLine(photo.ToString());
}
这是我在对问题的评论中建议的代码重构,将
updatePrice
标记为protectedvirtual以允许多态性,在ToString
中添加了.Name
,我删除了私有字段的无用访问器:
class Photo
{
private int width;
private int height;
protected double price;
public int Width {
get
{
return width;
}
set
{
width = value;
updatePrice();
}
}
public int Height {
get
{
return height;
}
set
{
height = value;
updatePrice();
}
}
public Photo(int width, int height)
{
this.width = width;
this.height = height;
updatePrice();
}
public virtual double Price
{
get
{
return price;
}
}
public override string ToString()
{
return GetType().Name +
" with a width of " + width +
" and a height of " + height +
" with a base price of " + Price.ToString("C2");
}
protected virtual void updatePrice()
{
if (width == 8 && height == 10)
price = 3.99;
else if (width == 10 && height == 12)
price = 5.99;
else
price = 9.99;
}
}
因此,您不需要从外部调用updatePrice:
static void Main()
{
Photo photo = new Photo(10, 12);
WriteLine(photo.ToString());
photo.height = 4;
WriteLine(photo.ToString());
}
ToString
应标记为override
。更新字段后,您可以调用UpdatePrice
,而不使用Width
和Height
设置程序中的参数,并且可以将其标记为private或protected。您可以在初始化字段之后从构造函数调用它,无需重复代码。您确定此代码可以编译吗?您正在调用私有属性height而不是public one height设置。当您在调试器中单步执行UpdatePrice
时,私有成员height将不会调用UpdatePrice方法。所有值是否都符合预期?是否在预期点调用它?ToString
应标记为override
。更新字段后,您可以调用UpdatePrice
,而不使用Width
和Height
设置器中的参数,并且可以将其标记为private或protected。您可以在初始化字段之后从构造函数调用它,无需重复代码。您确定此代码可以编译吗?您正在调用私有属性height而不是public one height设置。当您在调试器中单步执行UpdatePrice
时,私有成员height将不会调用UpdatePrice方法。所有值是否都符合预期?是否在预期点调用它?它将计算每次的价格,因此与仅在其他属性更改时设置相比,它是速度性能损失。它将计算每次的价格,因此与仅在其他属性更改时设置相比,它是速度性能损失。如您所示在您的评论中建议,updatePrice
应为private
或protected
。正如您在评论中建议的,updatePrice
应为private
或protected
。