exemple d'applet Java construire un objet en surchargeant le constructeur

import java.applet.*;
import java.awt.*;

public class
Code1 extends Applet   {
    double
tension;
    double
impedance;
    public void paint (Graphics
g) {
       
Courant Circuit1 = new Courant(); // définit un objet Courant (sans paramètre) de la classe Courant
        double
reponse1= Circuit1.CalculI() ;// reçoit en retour la valeur 230/23 soit reponse1 contient le double 10
        String
CourantS1 = Double.toString(reponse1); // conversion pour afficher
      
g.drawString(CourantS1,5,50);
       
Courant Circuit2 = new Courant (111,37); // définit un objet Courant (avec paramètre) de la classe Courant
        double
reponse2 = Circuit2.CalculI(); // reçoit en retour la valeur 111/37 soit reponse2 contient le double 3
        String
CourantS2 = Double.toString(reponse2);
   
    g.drawString("I = "+ CourantS2 +"[A]",5,150);
    }
     public class
Courant  {
         public
Courant()  {                               // construit sans  valeurs transmises
               
tension = 230;
               
impedance = 23;
            }
            public
Courant(double t, double i)  {   // construit avec les valeurs transmises
               
tension = t;
               
impedance = i;
            }
            public double CalculI()    {                      //
double car la valeur renvoyée est double
            return
tension / impedance;
            }
     }
}


installes donc JAVA !
import java.applet.*;
import java.awt.*;

public class
Code3 extends Applet {
    double
tension;
    double
impedance;
    String
type;

    public void paint (Graphics
g) {
       
Courant Circuit1 = new Courant();
        double
reponse1= Circuit1.CalculI();
        String
CourantS1 = Double.toString(reponse1);
       
g.drawString(CourantS1,5,50);
        String
type1=Circuit1.Type();
       
g.drawString(type1,5,70);

       
Courant Circuit2 = new Courant (111,37);
        double reponse2=
Circuit2.CalculI();
        String
CourantS2 = Double.toString(reponse2);
       
g.drawString("I = "+ CourantS2 +"[A]",5,150);
       
g.drawString(Circuit2.Type(),5,170);
       
g.drawString(Circuit2.comment1,5,80);
       
g.drawString(Circuit2.comment2,5,90);
    }
        public class
Courant {
                        String
comment1, comment2;

            public
Courant() {
               
tension = 230;
               
impedance = 23;
                                this.
comment1="circuit 2";
            this.comment2="
lois d'ohm";
            }
            public
Courant(double t, double i) {
               
tension = t;
               
impedance = i;
            this.
comment1="circuit 2";
            this.
comment2="lois d'ohm2";

            }
            public double CalculI() // double car la valeur renvoyée est double
            {
            return
tension / impedance;
            }
            public String
Type(){

                if (
t == 230)
                    type = "
par defaut";
                else
                    type="
calcule";
            return
type;
            }

}
}

ici un coustructeur surchargé avec plusieurs méthode installes donc JAVA !
titre.gif (1044 octets)

dsdem.gif (1088 octets)