表达动态多态性
Expressing dynamic polymorphism
根据用户输入的动物危险等级,calculate_insurance
方法将select正确的大小写和运行计算以确定动物保险的费用.
危险等级在父 class 中设置
保险计算是在具有 动态多态性 的 2 个子 class 中通过覆盖父 class 来完成的。解决这个问题的最佳方法是什么?非常感谢您的代码解释
基础class-
public abstract class Animal
{
protected int danger_rating = 0;
//get danger rating from user (1-5)
while (true)
{
Console.WriteLine("What is the animals danger rating? \nSelect a number between 1 - 5: "); //Prompt user to type in animals danger rating
string input = Console.ReadLine(); //reads string input
if (int.TryParse(input, out danger_rating)) //convert user string input to int danger_rating
{
if (danger_rating == 1) //loop until danger_rating is between 1 and 5
{
break;
}
else if (danger_rating == 2)
{
break;
}
else if (danger_rating == 3)
{
break;
}
else if (danger_rating == 4)
{
break;
}
else if (danger_rating == 5)
{
break;
}
else
{
Console.WriteLine("Invalid Number. \nNumber must be between 1 - 5");
}
}
else
{
Console.WriteLine("This is not a number");
}
public virtual double calculate_insurance(int danger_rating)
{
return 0;
}
}
哺乳动物class -
public class Mammal : Animal //Derived class
{
public virtual double calculate_insurance(int danger_rating)
{
int ins_base = 5000; //base for mammals
double Insurance; //this will contain the output
//Select case based on danger rating 1 - 5
switch (danger_rating)
{
case 1:
Insurance = (ins_base * 10) * 0.02;
break;
case 2:
Insurance = (ins_base * 20) * 0.05;
break;
case 3:
Insurance = (ins_base * 30) * 0.05;
break;
case 4:
Insurance = (ins_base * 40) * 0.05;
break;
case 5:
Insurance = (ins_base * 50) * 0.10;
break;
default:
Console.WriteLine("Invalid danger rating");
break;
} //end of switch
return base.calculate_insurance(danger_rating);
}
}
爬虫类class -
public class Reptile : Animal //Derived class
{
public virtual double calculate_insurance(int danger_rating)
{
int ins_base = 1000; //base for reptiles
double Insurance; //this will contain the output
//Select case based on danger rating 1 - 5
switch (danger_rating)
{
case 1:
Insurance = (ins_base * 10) * 0.02;
break;
case 2:
Insurance = (ins_base * 20) * 0.05;
break;
case 3:
Insurance = (ins_base * 30) * 0.05;
break;
case 4:
Insurance = (ins_base * 40) * 0.05;
break;
case 5:
Insurance = (ins_base * 50) * 0.10;
break;
default:
Console.WriteLine("Invalid danger rating");
break;
} //end of switch
return base.calculate_insurance(danger_rating);
}
}
我不太确定是否有问题需要“解决”,除了在方法级别浮动的 while 循环的基础 class 中的语法错误以及计算保险的一些不可靠之处派生类型的值,然后 return 基本类型的保险计算(即 0)。
哺乳动物和爬行动物中的 calculate_insurance
方法应声明为 override
基本方法,并应以 return Insurance
结尾(注意;应称为 insurance
; 我们不使用 PascalCase 命名局部变量) 不 returning 调用基数的结果..
如果您正在询问如何使用您创建的内容。这种性质的多态性背后的想法是,您可以拥有一个基本类型的变量,而不知道或不关心其中存储的实际类型是什么,因为您只使用基本类型声明存在的功能
Animal a = new Mammal();
a.calculate_insurance(1); //returns 1000, the rating for a Mammal of type 1
Animal b = new Reptile();
b.calculate_insurance(1); //returns 200, the rating for a Reptile of type 1
根据用户输入的动物危险等级,calculate_insurance
方法将select正确的大小写和运行计算以确定动物保险的费用.
危险等级在父 class 中设置
保险计算是在具有 动态多态性 的 2 个子 class 中通过覆盖父 class 来完成的。解决这个问题的最佳方法是什么?非常感谢您的代码解释
基础class-
public abstract class Animal
{
protected int danger_rating = 0;
//get danger rating from user (1-5)
while (true)
{
Console.WriteLine("What is the animals danger rating? \nSelect a number between 1 - 5: "); //Prompt user to type in animals danger rating
string input = Console.ReadLine(); //reads string input
if (int.TryParse(input, out danger_rating)) //convert user string input to int danger_rating
{
if (danger_rating == 1) //loop until danger_rating is between 1 and 5
{
break;
}
else if (danger_rating == 2)
{
break;
}
else if (danger_rating == 3)
{
break;
}
else if (danger_rating == 4)
{
break;
}
else if (danger_rating == 5)
{
break;
}
else
{
Console.WriteLine("Invalid Number. \nNumber must be between 1 - 5");
}
}
else
{
Console.WriteLine("This is not a number");
}
public virtual double calculate_insurance(int danger_rating)
{
return 0;
}
}
哺乳动物class -
public class Mammal : Animal //Derived class
{
public virtual double calculate_insurance(int danger_rating)
{
int ins_base = 5000; //base for mammals
double Insurance; //this will contain the output
//Select case based on danger rating 1 - 5
switch (danger_rating)
{
case 1:
Insurance = (ins_base * 10) * 0.02;
break;
case 2:
Insurance = (ins_base * 20) * 0.05;
break;
case 3:
Insurance = (ins_base * 30) * 0.05;
break;
case 4:
Insurance = (ins_base * 40) * 0.05;
break;
case 5:
Insurance = (ins_base * 50) * 0.10;
break;
default:
Console.WriteLine("Invalid danger rating");
break;
} //end of switch
return base.calculate_insurance(danger_rating);
}
}
爬虫类class -
public class Reptile : Animal //Derived class
{
public virtual double calculate_insurance(int danger_rating)
{
int ins_base = 1000; //base for reptiles
double Insurance; //this will contain the output
//Select case based on danger rating 1 - 5
switch (danger_rating)
{
case 1:
Insurance = (ins_base * 10) * 0.02;
break;
case 2:
Insurance = (ins_base * 20) * 0.05;
break;
case 3:
Insurance = (ins_base * 30) * 0.05;
break;
case 4:
Insurance = (ins_base * 40) * 0.05;
break;
case 5:
Insurance = (ins_base * 50) * 0.10;
break;
default:
Console.WriteLine("Invalid danger rating");
break;
} //end of switch
return base.calculate_insurance(danger_rating);
}
}
我不太确定是否有问题需要“解决”,除了在方法级别浮动的 while 循环的基础 class 中的语法错误以及计算保险的一些不可靠之处派生类型的值,然后 return 基本类型的保险计算(即 0)。
哺乳动物和爬行动物中的 calculate_insurance
方法应声明为 override
基本方法,并应以 return Insurance
结尾(注意;应称为 insurance
; 我们不使用 PascalCase 命名局部变量) 不 returning 调用基数的结果..
如果您正在询问如何使用您创建的内容。这种性质的多态性背后的想法是,您可以拥有一个基本类型的变量,而不知道或不关心其中存储的实际类型是什么,因为您只使用基本类型声明存在的功能
Animal a = new Mammal();
a.calculate_insurance(1); //returns 1000, the rating for a Mammal of type 1
Animal b = new Reptile();
b.calculate_insurance(1); //returns 200, the rating for a Reptile of type 1