メンバー関数(giveCharityなど)を使用してクラス(Personなど)を作成したいのですが、人工知能を模倣して、クラスのインスタンスごとにそのメソッドの内容を変えたいと考えています。これは可能ですか?各インスタンスのメソッドのコードをいつどのように入力しますか?
次に例を示します。
public class Person
{
// data members
private int myNumOfKids;
private int myIncome;
private int myCash;
// constructor
public Person(int kids, int income, int cash)
{
myNumOfKids = kids;
myIncome = income;
myCash = cash;
}
// member function in question
public int giveCharity(Person friend)
{
int myCharity;
// This is where I want to input different code for each person
// that determines how much charity they will give their friend
// based on their friend's info (kids, income, cash, etc...),
// as well as their own tendency for compassion.
myCash -= myCharity;
return myCharity;
}
}
Person John = new Person(0, 35000, 500);
Person Gary = new Person(3, 40000, 100);
// John gives Gary some charity
Gary.myCash += John.giveCharity(Gary);