PDA

View Full Version : مبتدی: چند ریختی



parsa lotfy
دوشنبه 03 اسفند 1394, 18:11 عصر
با سلام...
من در کتاب دیتل فصل توارث رو تمووم کردم ، حالا در اول فصل چند ریختی ی مشکلی پیدا کردم که نمیتوونم ازش رد بشم...

اوونم این تکه کد هستش که مفهوومش رو متوجه نمیشم :متفکر::متفکر:

// invoke ToString and Earnings on derived class object
// using base class variable
CommissionEmployee commissionEmployee2 =
basePlusCommissionEmployee;

البته این قسمتی هستش که متوجه نمیشم ... ینی انتظار داشتم که به این شکل باشه :

// invoke ToString and Earnings on derived class object
// using base class variable
CommissionEmployee commissionEmployee2 = new CommissionEmployee();


ولی اوون شکلشو تا حالا ندیده بوودم :متفکر::متفکر:

اگ زحمت نیس برام توضیح بدین :قلب::قلب::قلب:

با تشکر...

اینم کد کل برنامس :

کلاس تست برنامه :

using System;

public class PolymorphismTest
{
public static void Main( string[] args )
{
// assign base class reference to base class variable
CommissionEmployee commissionEmployee = new CommissionEmployee(
"Sue", "Jones", "222-22-2222", 10000.00M, .06M );

// assign derived class reference to derived class variable
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee( "Bob", "Lewis",
"333-33-3333", 5000.00M, .04M, 300.00M );

// invoke ToString and Earnings on base class object
// using base class variable
Console.WriteLine( "{0} {1}:\n\n{2}\n{3}: {4:C}\n",
"Call CommissionEmployee's ToString and Earnings methods",
"with base class reference to base class object",
commissionEmployee.ToString(),
"earnings", commissionEmployee.Earnings() );

// invoke ToString and Earnings on derived class object
// using derived class variable
Console.WriteLine( "{0} {1}:\n\n{2}\n{3}: {4:C}\n",
"Call BasePlusCommissionEmployee's ToString and Earnings",
"methods with derived class reference to derived class object",
basePlusCommissionEmployee.ToString(),
"earnings", basePlusCommissionEmployee.Earnings() );

// invoke ToString and Earnings on derived class object
// using base class variable
CommissionEmployee commissionEmployee2 =
basePlusCommissionEmployee;
Console.WriteLine( "{0} {1}:\n\n{2}\n{3}: {4:C}",
"Call BasePlusCommissionEmployee's ToString and Earnings",
"methods with base class reference to derived class object",
commissionEmployee2.ToString(), "earnings",
commissionEmployee2.Earnings() );
} // end Main
} // end class PolymorphismTest



کلاس Base :

using System;

public class CommissionEmployee
{
private string firstName;
private string lastName;
private string socialSecurityNumber;
private decimal grossSales; // gross weekly sales
private decimal commissionRate; // commission percentage

// five-parameter constructor
public CommissionEmployee( string first, string last, string ssn,
decimal sales, decimal rate )
{
// implicit call to object constructor occurs here
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
GrossSales = sales; // validate gross sales via property
CommissionRate = rate; // validate commission rate via property
} // end five-parameter CommissionEmployee constructor

// read-only property that gets commission employee's first name
public string FirstName
{
get
{
return firstName;
} // end get
} // end property FirstName

// read-only property that gets commission employee's last name
public string LastName
{
get
{
return lastName;
} // end get
} // end property LastName

// read-only property that gets
// commission employee's social security number
public string SocialSecurityNumber
{
get
{
return socialSecurityNumber;
} // end get
} // end property SocialSecurityNumber

// property that gets and sets commission employee's gross sales
public decimal GrossSales
{
get
{
return grossSales;
} // end get
set
{
if ( value >= 0 )
grossSales = value;
else
throw new ArgumentOutOfRangeException(
"GrossSales", value, "GrossSales must be >= 0" );
} // end set
} // end property GrossSales

// property that gets and sets commission employee's commission rate
public decimal CommissionRate
{
get
{
return commissionRate;
} // end get
set
{
if ( value > 0 && value < 1 )
commissionRate = value;
else
throw new ArgumentOutOfRangeException( "CommissionRate",
value, "CommissionRate must be > 0 and < 1" );
} // end set
} // end property CommissionRate

// calculate commission employee's pay
public virtual decimal Earnings()
{
return CommissionRate * GrossSales;
} // end method Earnings

// return string representation of CommissionEmployee object
public override string ToString()
{
return string.Format(
"{0}: {1} {2}\n{3}: {4}\n{5}: {6:C}\n{7}: {8:F2}",
"commission employee", FirstName, LastName,
"social security number", SocialSecurityNumber,
"gross sales", GrossSales, "commission rate", CommissionRate );
} // end method ToString
} // end class CommissionEmployee


کلاس Derived :

using System;

public class BasePlusCommissionEmployee : CommissionEmployee
{
private decimal baseSalary; // base salary per week

// six-parameter derived class constructor
// with call to base class CommissionEmployee constructor
public BasePlusCommissionEmployee( string first, string last,
string ssn, decimal sales, decimal rate, decimal salary )
: base( first, last, ssn, sales, rate )
{
BaseSalary = salary; // validate base salary via property
} // end six-parameter BasePlusCommissionEmployee constructor

// property that gets and sets
// BasePlusCommissionEmployee's base salary
public decimal BaseSalary
{
get
{
return baseSalary;
} // end get
set
{
if ( value >= 0 )
baseSalary = value;
else
throw new ArgumentOutOfRangeException( "BaseSalary",
value, "BaseSalary must be >= 0" );
} // end set
} // end property BaseSalary

// calculate earnings
public override decimal Earnings()
{
return BaseSalary + base.Earnings();
} // end method Earnings

// return string representation of BasePlusCommissionEmployee
public override string ToString()
{
return string.Format( "base-salaried {0}\nbase salary: {1:C}",
base.ToString(), BaseSalary );
} // end method ToString
} // end class BasePlusCommissionEmployee

parsa lotfy
سه شنبه 04 اسفند 1394, 12:23 عصر
کسی نبوود توضیح بده ؟!

به خدا کل کد رو نمیخواااماااا ، اوون قسمته خاصشو که بالا گفتم رو نمیفهمم...

اگ ممکنه توضیح بدین :قلب::قلب::قلب::قلب: