C# - Oggetto Studente estende Persona (Codice)

Come creare l'oggetto Studente che estende Persona :
CODICE C# : (File : Persona.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProgrammaConsol { class Persona : IComparable<Persona>, IComparable { private String nome; private String cognome; private int eta; private String codice; public Persona(String nome, String cognome, int eta, String codice) { this.nome = nome; this.cognome = cognome; this.eta = eta; this.codice = codice; } public String Nome { get { return this.nome; } set { this.nome = value; } } public String Cognome { get { return this.cognome; } set { this.cognome = value; } } public int Eta { get { return this.eta; } set { this.eta = value; } } public String Codice { get { return this.codice; } set { this.codice = value; } } public Boolean equals(Object o) { Persona persona = (Persona)o; return( this.codice.Equals(persona.codice)&& this.nome.Equals(persona.nome) && this.cognome.Equals(persona.nome) && this.eta == persona.eta ); } public int hashCode() { return this.eta; } public int CompareTo(Persona other) { // Should be a null check here... return this.eta.CompareTo(other.eta); } public int CompareTo(object obj) { // Should be a null check here... var otherPerson = obj as Persona; if (otherPerson == null) throw new ArgumentException("..."); // Call the generic interface's implementation: return CompareTo(otherPerson); } } }
Adesso creiamo l'oggetto Studente che estende Persona :
CODICE C# : (File : Studente.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProgrammaConsol { class Studente : Persona, IComparable<Studente>, IComparable { private String matricola; private String nomeUniversita; private String nomeCorsoStudi; public Studente(String nome, String cognome, int eta, String codice,String matricola,String nomeUniversita, String nomeCorsoStudi): base(nome, cognome, eta, codice) { this.matricola = matricola; this.nomeUniversita = nomeUniversita; this.nomeCorsoStudi = nomeCorsoStudi; } public String Matricola { get { return this.matricola; } set { this.matricola = value; } } public String NomeUniversita { get { return this.nomeUniversita; } set { this.nomeUniversita = value; } } public String NomeCorsoStudi { get { return this.nomeCorsoStudi; } set { this.nomeCorsoStudi = value; } } public int hashCode() { return (int)this.matricola.GetHashCode(); } public Boolean equals(Object o) { Studente studente = (Studente)o; return ( Codice.Equals(studente.Codice) && Nome.Equals(studente.Nome) && Cognome.Equals(studente.Cognome) && Eta == studente.Eta && this.matricola.Equals(studente.matricola) && this.nomeUniversita.Equals(studente.nomeUniversita) && this.nomeCorsoStudi.Equals(studente.nomeCorsoStudi) ); } public int CompareTo(Studente other) { // Should be a null check here... return this.matricola.CompareTo(other.matricola); } public int CompareTo(object obj) { // Should be a null check here... var otherPerson = obj as Studente; if (otherPerson == null) throw new ArgumentException("..."); // Call the generic interface's implementation: return CompareTo(otherPerson); } } }
Come richiamare gli oggetti Persona e Studente :
CODICE C# : (File : Program.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProgrammaConsol { class Program { static void Main(string[] args) { try { Persona persona = new Persona("Prova","Mimmo",10,"1110"); Persona persona1 = new Persona("sssss", "xxxxx", 33, "dw32"); Console.WriteLine("Nome {0} Cognome {1} Eta {2} Codice {3}",persona.Nome,persona.Cognome,persona.Eta,persona.Codice); Console.WriteLine("Nome {0} Cognome {1} Eta {2} Codice {3}", persona1.Nome, persona1.Cognome, persona1.Eta, persona1.Codice); Studente studente = new Studente("Marco", "Rossi", 10, "cod2rossi", "002332", "RomaTre", "Ingegneria Informatica"); Console.WriteLine("Nome {0} Cognome {1} Eta {2} Codice {3}", studente.Nome, studente.Cognome, studente.Eta, studente.Codice); Console.WriteLine("Matricola {0} NomeUniversita {1} CorsoDiStudi {2} ", studente.Matricola, studente.NomeUniversita, studente.NomeCorsoStudi); }catch (System.ApplicationException ex){ //Controllo dell'errori Console.WriteLine("Errori {0}", ex.Message); } finally{ Console.ReadLine(); } } } }

By ImaginSystems & Queen Gin