C# héritage
Résolu
gilles81
Messages postés
72
Statut
Membre
-
gilles81 Messages postés 72 Statut Membre -
gilles81 Messages postés 72 Statut Membre -
Bonjour,
j'ai implémenté ceci :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rayan2
{
class Publication
{
private String title;
private int sale;
private float price;
private int participation;
public String Title
{
get
{
return title;
}
set
{
title = value;
}
}
public int Sale
{
get
{
return sale;
}
set
{
sale = value;
}
}
public float Price
{
get
{
return price;
}
set
{
price = value;
}
}
public int Participation
{
get
{
return participation;
}
set
{
participation = sale * price;
Console.WriteLine("price als float: {0}, price als int: {1}", price, (int)price);
}
}
public override string ToString()
{
return String.Format("{0}\n----------------------\nTitel : {1}\nVerkaufszahlen : {2}\nPreis pro Stück ($) : {3}\nBeteiligungssatz : {4} %", title, sale, price, participation);
}
public virtual float CalculateFee()
{
float calculatefee = (float)(sale * (int)price / 100 * participation);
return calculatefee;
}
}
class Book : Publication
{
private int pages;
public int Pages
{
get
{
return pages;
}
set
{
pages = value;
}
}
public override string ToString()
{
return String.Format("{0}\n----------------------\nTitel : {1}\nVerkaufszahlen : {2}\nPreis pro Stück ($) : {3}\nBeteiligungssatz : {4} %\nPages : {5}", title, sale, price, participation,pages);
}
}
abstract class DigitalMediaj : Publication
{
private const float literalPropertyFee = 10000;
private int runTime;
public DigitalMediaj(int runTime)
{
this.runTime = runTime;
}
public float LiteralPropertyFee
{
get
{
return LiteralPropertyFee;
}
}
private int RuntimeMedium{
get
{
return runTime ;
}
set
{
runTime = value ;
}
}
public override float CalculateFee()
{
float calculatefee = (float)((sale * (int)price / 100 * participation)+(int) LiteralPropertyFee);
return calculatefee;
}
}
class Audio : DigitalMediaj
{
}
public class Movie : DigitalMediaj
{
private float movieRights;
public float MovieRights
{
get
{
return MovieRights;
}
set
{
MovieRights = value;
}
}
public override float CalculateFee()
{
float calculatefee = (float)(((sale * (int)price / 100 * participation) + (int)LiteralPropertyFee)+(int)MovieRights);
return calculatefee;
}
public override string ToString()
{
return String.Format("{0}{1}{2}{3}{4}{5}{6}", title, sale, price, participation, LiteralPropertyFee,MovieRights);
}
}
}
En fait je dois implémenter la méhode CalculateFee() qu'une seule fois , c'est à dire dans la classe Publication dont hérite la class Book et la classe DigitalMediaj. Les classes Audio et Movie heritent de la classe DigitalMediaj. Comment devrais-je utiliser la méthode CalculateFee() dans les autres classes sans la dupliquer.
En plus il ya une erreur de compilation qui persiste: Inconsistent accessibility: base class 'Rayan2.DigitalMediaj' is less accessible than class 'Rayan2.Movie'
merci
j'ai implémenté ceci :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rayan2
{
class Publication
{
private String title;
private int sale;
private float price;
private int participation;
public String Title
{
get
{
return title;
}
set
{
title = value;
}
}
public int Sale
{
get
{
return sale;
}
set
{
sale = value;
}
}
public float Price
{
get
{
return price;
}
set
{
price = value;
}
}
public int Participation
{
get
{
return participation;
}
set
{
participation = sale * price;
Console.WriteLine("price als float: {0}, price als int: {1}", price, (int)price);
}
}
public override string ToString()
{
return String.Format("{0}\n----------------------\nTitel : {1}\nVerkaufszahlen : {2}\nPreis pro Stück ($) : {3}\nBeteiligungssatz : {4} %", title, sale, price, participation);
}
public virtual float CalculateFee()
{
float calculatefee = (float)(sale * (int)price / 100 * participation);
return calculatefee;
}
}
class Book : Publication
{
private int pages;
public int Pages
{
get
{
return pages;
}
set
{
pages = value;
}
}
public override string ToString()
{
return String.Format("{0}\n----------------------\nTitel : {1}\nVerkaufszahlen : {2}\nPreis pro Stück ($) : {3}\nBeteiligungssatz : {4} %\nPages : {5}", title, sale, price, participation,pages);
}
}
abstract class DigitalMediaj : Publication
{
private const float literalPropertyFee = 10000;
private int runTime;
public DigitalMediaj(int runTime)
{
this.runTime = runTime;
}
public float LiteralPropertyFee
{
get
{
return LiteralPropertyFee;
}
}
private int RuntimeMedium{
get
{
return runTime ;
}
set
{
runTime = value ;
}
}
public override float CalculateFee()
{
float calculatefee = (float)((sale * (int)price / 100 * participation)+(int) LiteralPropertyFee);
return calculatefee;
}
}
class Audio : DigitalMediaj
{
}
public class Movie : DigitalMediaj
{
private float movieRights;
public float MovieRights
{
get
{
return MovieRights;
}
set
{
MovieRights = value;
}
}
public override float CalculateFee()
{
float calculatefee = (float)(((sale * (int)price / 100 * participation) + (int)LiteralPropertyFee)+(int)MovieRights);
return calculatefee;
}
public override string ToString()
{
return String.Format("{0}{1}{2}{3}{4}{5}{6}", title, sale, price, participation, LiteralPropertyFee,MovieRights);
}
}
}
En fait je dois implémenter la méhode CalculateFee() qu'une seule fois , c'est à dire dans la classe Publication dont hérite la class Book et la classe DigitalMediaj. Les classes Audio et Movie heritent de la classe DigitalMediaj. Comment devrais-je utiliser la méthode CalculateFee() dans les autres classes sans la dupliquer.
En plus il ya une erreur de compilation qui persiste: Inconsistent accessibility: base class 'Rayan2.DigitalMediaj' is less accessible than class 'Rayan2.Movie'
merci
2 réponses
Bon voilà j'ai apporter quelques modifications :
Sinon je te conseille de remplacer ceci :
Par ceci (étant donné que tu ne modifie jamais la valeur de la participation avec value.
Tu peux y rajouter ton affichage console et ainsi supprimer le _participation, comme ça tu aura toujours la valeur de Sale * Price quand tu demandera Participation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rayan2
{
class Publication
{
private String _title;
private int _sale;
private float _price;
private int _participation;
public String Title
{
get { return _title; }
set { _title = value; }
}
public int Sale
{
get { return _sale; }
set { _sale = value; }
}
public float Price
{
get { return _price; }
set { _price = value; }
}
public int Participation
{
get { return _participation; }
set
{
_participation = (int)(_sale * _price);
Console.WriteLine("price als float: {0}, price als int: {1}", price, (int)price);
}
}
public override string ToString()
{
return String.Format("{0}\n----------------------\nTitel : {1}\nVerkaufszahlen : {2}\nPreis pro Stück ($) : {3}\nBeteiligungssatz : {4} %", Title, Sale, Price, Participation);
}
public virtual float CalculateFee()
{
float calculatefee = (float)(Sale * (int)Price / 100 * Participation);
return calculatefee;
}
}
class Book : Publication
{
private int _pages;
public int Pages
{
get { return _pages; }
set { _pages = value; }
}
public override string ToString()
{
return String.Format("{0}\n----------------------\nTitel : {1}\nVerkaufszahlen : {2}\nPreis pro Stück ($) : {3}\nBeteiligungssatz : {4} %\nPages : {5}", base.Title, base.Sale, base.Price, base.Participation, Pages);
}
}
abstract class DigitalMediaj : Publication
{
private const float _literalPropertyFee = 10000;
private int _runTime;
public DigitalMediaj(int runTime)
{
this._runTime = runTime;
}
public float LiteralPropertyFee
{
get { return _literalPropertyFee; }
}
private int RuntimeMedium
{
get { return _runTime; }
set { _runTime = value; }
}
public override float CalculateFee()
{
return (base.CalculateFee() + (int)LiteralPropertyFee);
}
}
class Audio : DigitalMediaj
{
}
public class Movie : DigitalMediaj
{
private float _movieRights;
public float MovieRights
{
get { return _movieRights; }
set { _movieRights = value; }
}
public override float CalculateFee()
{
return (base.CalculateFee() + (int)MovieRights);
}
public override string ToString()
{
return base.ToString() + String.Format("{0}{1}", base.LiteralPropertyFee, MovieRights);
}
}
}
Sinon je te conseille de remplacer ceci :
public int Participation
{
get { return _participation; }
set
{
_participation = (int)(_sale * _price);
Console.WriteLine("price als float: {0}, price als int: {1}", price, (int)price);
}
}
Par ceci (étant donné que tu ne modifie jamais la valeur de la participation avec value.
public int Participation
{
get { return (int)(Sale * Price); }
}
Tu peux y rajouter ton affichage console et ainsi supprimer le _participation, comme ça tu aura toujours la valeur de Sale * Price quand tu demandera Participation.