Posez votre question Signaler

C# héritage [Résolu]

gilles81 68Messages postés 20 mai 2008Date d'inscription - Dernière réponse le 26 déc. 2008 à 12:26
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
Lire la suite 

C# héritage »

2 réponses
Réponse
+0
moins plus
Bon voilà j'ai apporter quelques modifications :

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.
Ajouter un commentaire
Réponse
+0
moins plus
merci
Ajouter un commentaire
Ce document intitulé « c# héritage » issu de CommentCaMarche (www.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.
Dossier à la une
5 extensions si vous voulez revenir à l'ancien Facebook