andaohiresakaobjetfahaxiv [wiki ifg]
Writing /home/infogasy/domains/wiki.infogasy.net/public_html/data/cache/9/9f4ed30f38aa806f32978fa81e325afd.xhtml failed

Andao Hiresaka Objet indray faha XIV

Ohatra fampiharana amin'ny fanorenana manao andalan-tohatra (tohiny) :

Raha jerena akaiky moa ilay vokatra azontsika tamin’ny fampiasana faha-13 teo dia:

Hita aloha fa mandeha tsara ilay izy ? saingy, misy indro kely hoy ny mpandrafitra:
Moa ve tena voahaja tsara ilay toetra tsy maintsy arahina rehefa manorina miankina amin’ny fitsipika andalan-tohatra tao amin’io kilasy SehoLahasoratra.java io ?

Ohatra akaiky: ilay andalana soratra :

case 3:
    tahirySoratra.setText(tahiryMaharitra
                              .mamakyTahiry()
                              .toString());
    break;

tahirySoratra zava-madinika anaty Fanehoana, nefa tahiryMaharitra Objet avy amin’ny Fitehirizina maharitra !!!

Andao ary hovaintsika indray ilay kisarisary mpanampy (fomba UML) teo raha izany no izy e :

1. Dia vokatr?io sary io ary dia tsy maintsy kitihina indray ary izany ilay kilasy ZavatraAzoKajiana.java; na ny marina miampy toetra sy hetsika ao anatiny.

Composition ihany ary ny fiaraha-miasan’ny TahirySoratraASCII.java aminy eto:

package resakaObjet;
 
import resakaObjet.TahirySoratraASCII;
 
public class ZavatraAzoKajiana
{
    private Object sanda;
    private TahirySoratraASCII tahiry;
 
    public ZavatraAzoKajiana()
    {
        sanda = new Object();
        tahiry = new TahirySoratraASCII(".","tahiry.dat","");
    }
 
    public ZavatraAzoKajiana(Object sandaHafa)
    {
        sanda = sandaHafa;
    }
 
    public Object getSanda()
    {
        return sanda;
    }
 
    public void setSanda(Object sandaHafa)
    {
        sanda = sandaHafa;
    }
 
    public TahirySoratraASCII getTahiry()
    {
        return tahiry;
    }
 
    public void setTahiryZavatra(Object zavatra)
    {
        setSanda(zavatra);
        tahiry.manoratraTahiry(zavatra);
    }
 
    public Object getTahiryZavatra()
    {
        setSanda(tahiry.mamakyTahiry());
        return sanda;
    }
 
    public Object kajyAdisanina(double sandaHafa) throws ResakaObjetException
    {
        if(getSanda().getClass() != Double.class) throw new ResakaObjetException(getSanda());
        Double d = (Double)getSanda();
        return new Double(d.doubleValue()+sandaHafa) ;
    }
 
    public Object kajyAdisanina(String sandaHafa) throws ResakaObjetException
    {
        if(getSanda().getClass() != String.class) throw new ResakaObjetException(getSanda());
        String s = (String)getSanda();
        return s.concat(sandaHafa);
    }
 
    public String toString()
    {
        return sanda.toString();
    }
 
    static public void main(String arg[]) {
        ZavatraAzoKajiana test = new ZavatraAzoKajiana();
        test.setTahiryZavatra(new Integer(9999*12).toString());
        test.getTahiryZavatra();
        System.out.println(test.getSanda());
    }
}

Fanazavana:

Ataontsika amin’ny soratra matavy ary ireo andalan-tsoratra nampiana ao anatin?ity kilasy ZavatraAzoKajiana.java ity.

private TahirySoratraASCII tahiry;

Ahafahana mifandray amin’ny andalan-tohatra fitehirizana TahirySoratraASCII.java

public ZavatraAzoKajiana()
    {
        sanda = new Object();
        tahiry = new TahirySoratraASCII(".","tahiry.dat","");
    }

Constructeur tsy misy argument aloha no ataontsika eto (atao tsotra), nefa tsara tokoa io raha asiana argument izay mametraka avy hatrany ny toerana, ny anarana fichier (fampiharana tena izy)

public TahirySoratraASCII getTahiry()
  {
    return tahiry;
  }

Getters ilaina raha te haka ilay Objet mampifandray amin’ilay fitehirizana maharitra isika.

public void setTahiryZavatra(Object zavatra)
    {
        setSanda(zavatra);
        tahiry.manoratraTahiry(zavatra);
    }
  
    public Object getTahiryZavatra()
    {
        setSanda(tahiry.mamakyTahiry());
        return sanda;
    }

Eto no soratana sy arahina ilay toetra milaza hoe: ?ny setters ny kilasy iray dia tokony hanaiky arguments avy amin’ny getters ny kilasy izay mifanakaiky aminy ao amin’ilay andalan-tohatra napetraka?

  • Ilay hetsika setTahiryZavatra(Object) dia mandefa anaty tahiry maharitra (andalana TahirySoratraASCII.java) ny zavatra (karazana Object) izay ampidirina ho argument ao aminy (ny getSanda() moa dia manome vokatra karazana Object koa ao @ kilasy ZavatraAzoKajiana.java). Etsy andanina dia mametraka anaty sanda attribut-ny kilasy ZavatraAzoKajiana.java, izay nalefa anaty tahiry maharitra koa io hetsika io.
  • Ilay hetsika getTahiryZavatra() dia mamerina vokatra karazana Object, izay nalaina avy anaty tahiry maharitra ao amin’ity kilasy ZavatraAzoKajiana.java ity. Dia mametraka koa ilay Objet nalaina tanaty tahiry maharitra ho ao anaty attribut sanda koa izy.

Ilay main(String arg[]) moa dia fanandramana ny kilasintsika fotsiny no ilaina azy eto an !

2. Manaraka izay koa moa dia tsy maintsy kitihina koa izany ilay kilasy SehoLahasoratra.java, hifanaraka amin’ilay kisarisary nasehontsika tetsy ambony.
Mbola Composition ihany koa ny fiaraha-miasan’ny kilasy ZavatraAzoKajiana.java amin’ny SehoLahasoratra.java

package resakaObjet;
 
import java.awt.*;
import java.awt.event.* ;
import javax.swing.*;
import resakaObjet.ZavatraAzoKajiana;
 
public class SehoLahasoratra extends Frame
{
    Label lohateny1, lohateny2;
    Button bokotra1, bokotra2, bokotra3;
    TextArea tahirySoratra;
    TextArea lahasoratra;
 
    private ZavatraAzoKajiana zavatra; // Fifandraisana amin'ny kilasy ZavatraAzoKajiana.java
 
    public SehoLahasoratra()
    {
        super("Fanoratana lahasoratra");
        lohateny1 = new Label("Lahasoratra taloha :");
        tahirySoratra = new TextArea(20,100);
        lohateny2 = new Label("Lahasoratra vaovao :");
        lahasoratra = new TextArea(20,100);
        bokotra1 = new Button("Alefaso");
        bokotra2 = new Button("Fafao");
        bokotra3 = new Button("Vakio");
 
        zavatra = new ZavatraAzoKajiana(); // Manorina ilay Objet zavatra
 
        setLayout(new lahasoratraLayout());
 
        add(lohateny1);          add(bokotra3); add(tahirySoratra);
        add(lohateny2); add(lahasoratra);
        add(bokotra1); add(bokotra2);
 
        pack();
        addWindowListener(this.new RindraToerana());
    }
 
    public TextArea getTahirySoratra()
    {
        return tahirySoratra;
    }
 
    public ZavatraAzoKajiana getZavatra() // getters ny Objet zavatra
    {
        return zavatra;
    }
 
    public void setHainoTranga()
    {
        bokotra1.addActionListener(this.new RindraTranga());
        bokotra1.setActionCommand("1");
        bokotra2.addActionListener(this.new RindraTranga());
        bokotra2.setActionCommand("2");
        bokotra3.addActionListener(this.new RindraTranga());
        bokotra3.setActionCommand("3");
    }
 
    private class RindraTranga implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           String   action = e.getActionCommand();
           int choix = Integer.parseInt(action);
           switch(choix)
           {
               case 1: // tsindry bokotra Alefaso
                   try {
                       StringBuffer buf = (StringBuffer)zavatra.getTahiryZavatra();
      
                       zavatra.setSanda(buf.toString());
                       zavatra.setSanda(zavatra.kajyAdisanina("\n================\n"));
                       zavatra.setSanda(zavatra.kajyAdisanina(lahasoratra.getText()));
                       zavatra.setTahiryZavatra(zavatra.getSanda());
                       tahirySoratra.setText((String)zavatra.getSanda());
                       lahasoratra.setText("");
                   }
                   catch (Exception ex)
                   {
                       break;
                   }
                   break;
               case 2:
                   lahasoratra.setText("");
                   break;
               case 3: // Tsindry bokotra Vakio : manao downcasting, satria 
                       //fantatra mialoha fa StringBuffer no kirakiraina
                   tahirySoratra.setText(((StringBuffer)zavatra.getTahiryZavatra()).toString());
                   break;
           }
        }
    }
 
    private class lahasoratraLayout implements LayoutManager
    {
    
        public lahasoratraLayout()
        {
        }
    
        public void addLayoutComponent(String name, Component comp)
        {
        }
    
        public void removeLayoutComponent(Component comp)
        {
        }
    
        public Dimension preferredLayoutSize(Container parent)
        {
        Dimension dim = new Dimension(0, 0);
    
        Insets insets = parent.getInsets();
        dim.width = 494 + insets.left + insets.right;
        dim.height = 437 + insets.top + insets.bottom;
    
        return dim;
        }
    
        public Dimension minimumLayoutSize(Container parent)
        {
        Dimension dim = new Dimension(0, 0);
        return dim;
        if (c.isVisible()) {c.setBounds(insets.left+185,insets.top+8,72,24);}
        c = parent.getComponent(2);
        if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,472,152);}
        c = parent.getComponent(3);
        if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,184,24);}
        c = parent.getComponent(4);
        if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+232,480,160);}
        c = parent.getComponent(5);
        if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+400,72,24);}
        c = parent.getComponent(6);
        if (c.isVisible()) {c.setBounds(insets.left+104,insets.top+400,72,24);}
        }
    }
 
    private class RindraToerana extends WindowAdapter {
        public void windowClosing(WindowEvent ev)
        {
           System.exit(0);
        }
    }
 
static public void main(String arg[])
    {
        SehoLahasoratra test = new SehoLahasoratra();
        test.setHainoTranga();
        test.getTahirySoratra().setEditable(false);
        test.setVisible(true);
    }
}

Fanazavana:

Ataontsika soratra matavy koa ireo andalan-tsoratra niova anatin?ity kilasy ity; fa ny tena tokony hotadidiana rehefa mamaky io soruce io dia hoe:

Ny hetsika getTahiryZavatra() dia karazana StringBuffer ilay zavatra tena avoakany marina saingy voafono anaty karazana Object (upcasting). Tena zava-dehibe ny mahafantatra izay mialoha eto, satria ny zava-madinika anaty fanehoana dia tsy maintsy String no ekeny

    StringBuffer buf = (StringBuffer)zavatra.getTahiryZavatra();
    ...
    zavatra.setSanda(buf.toString());
    ...
    tahirySoratra.setText((String)zavatra.getSanda());
    ...
    tahirySoratra.setText(((StringBuffer)zavatra.getTahiryZavatra()).toString());

Ny ankoatra izay moa dia tsy toa izay voalaza tamin’ny resaka faha-13 no mbola mihatra eto. Toa izay koa ny fihodinan?ilay praograma efa vita, dia mitovy tsy misy valaka.

Izany hoe ny fanoratana na ny implementation no filaza azy no niova, mba hifanaraka amin’ny fitsipika miandalan-tohatra fotsiny e ?

(Hitohy ...)

 

andaohiresakaobjetfahaxiv.txt · Novaina farany:: 2008/09/11 12:43