1
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package texteditor;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;

/**
 *
 * @author
 */
public class TextPad implements DocumentListener,ChangeListener{
    private JTextPane textArea;
    private Document textDoc;

    private int selectionOffset;
    private int selectionLength;

    public void init()
    {
        //System.out.println("Constructor invoked");
        // TODO code application logic here
        JFrame window = new JFrame("Text Editor");
        //JMenuBar menuBar = window.getJMenuBar();

        /**
        //Create menu bar
        JMenuBar menuBar= new JMenuBar();
        //File Menu
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(new JMenuItem("Save"));
        * */

       // menuBar.add(fileMenu);
       //window.setJMenuBar(menuBar);

        this.textArea= new JTextPane();

        this.textDoc = this.textArea.getDocument();
        this.textDoc.addDocumentListener(this);
        this.textArea.getCaret().addChangeListener(this);
        //System.out.println(d.getClass());

        //override  default text generation ****THIS LINE******
       ((AbstractDocument)this.textDoc).getDocumentFilter();

        //Add scorllable interface in jtextpane
        window.add(new JScrollPane(this.textArea));

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
    public void changedUpdate(DocumentEvent e)
    {
       //System.out.println("changed");

    }
    public void removeUpdate(DocumentEvent e)
    {
          System.out.println("removed");
    }

    public void insertUpdate(DocumentEvent e)
    {

        try
        {
          System.out.println(this.textDoc.getText(0,this.textDoc.getLength()));

        }
        catch(Exception ex)
        {
            System.err.println(ex);
        }

    }
    /**
     *
     * @param e
     */
    public void stateChanged(ChangeEvent e)
    {
        System.out.println(this.textArea.getCaret().getMark());
    }

}

abstractDocumentas へのキャストは機能し((AbstractDocument)this.textDoc).getDocumentFilter();ますが、キャストしない this.textDoc.getDocumentFilter();とメソッドが見つからないというエラーがスローされるのはなぜですか。誰でも説明できますか?

編集:

if(this.textDoc instanceof AbstractDocument)
        {
            System.out.println("Yes it is");
        }

Yes it isまた、それが一種であることを意味するプリントAbstractDocument。メソッドを呼び出すとAbstractDocumentエラーがスローされる理由がわかりません。

4

2 に答える 2

2

HoverCraft はその通りです。Java では、オブジェクトのキャスト方法に従ってのみ、オブジェクトのメソッドとプロパティにアクセスできます。だから、実証するために

public interface Fooer {
    public int doFoo();
}

public class Foo implements Fooer{
    public int doFoo(){return 0;}
    public void doNoFooer(){}
}

public class Bar extends Foo {
    public void doYesFooer(){}
}

これらの定義を使用すると、次のようになります。

Foo foo = new Bar();
foo.doNoFooer(); // fine because it is a method of Foo
foo.doFoo();
foo.doYesFooer(); // causes an error because the variable is improperly typed.

Fooer fooer = foo;
fooer.doFoo(); // find because it is part of the definition of Fooer
fooer.doYesFoo(); // causes an error because the variable is improperly typed.
fooer.doNoFoo(); // causes an error because the variable is improperly typed.

Bar bar = (Bar) foo;
// notice that the next three do not cause errors.
bar.doYesFooer();
bar.doNoFooer();
bar.doFoo();
于 2013-01-09T04:14:01.323 に答える
0

cwallenpoole による以前の回答を拡張し、Java がこのように動作する理由を説明するには、次のコード スニペットを検討してください。

class Foo {
  int doFoo() {return 0;}
}

class Bar extends Foo {
  int doBar() {return 1;}
}

class FooBar {
  static int useBar(Foo foo) {
    ((Bar) foo).doBar();
  }
}

Bar someBar = new Bar();
Foo someFoo = new Foo();
FooBar.useBar(someBar) // compiles, no runtime error
FooBar.useBar(someFoo) // compiles, runtime error

Java の型システムは、2 番目の呼び出しをコンパイル時エラーにすることを意図しており、デバッグが難しい可能性のある実行時エラーではありません。明らかに、この例では、デバッグすることや、場合によっては instanceof チェックを追加することは難しくありません。ただし、キャストを使用すると、型システムが無効になり、さらに、型システムがプログラマーに提供するコンパイル時の安全性が無効になります。これは、そもそも静的に型付けされた言語を使用する主な理由の 1 つです。

明確に言うと、以下はコンパイル時エラーであり、変数の型に基づいて、変数がサブクラスで定義されていても、必ずしもその関数を持っているとは限らないことをプログラマーに警告しています。

class FooBar {
  static int useBar(Foo foo) {
    foo.doBar();
  }
}
于 2013-01-09T07:43:03.037 に答える