自学javaノートday 8(継承下)
8710 ワード
知識点:多態の概要、多態の拡張性、多態の転換、多態メンバーの特徴、Obejecクラス-equals()、Objectクラス-toString()
マルチステート
定義:ある種類の事物が存在する多種の存在形態.
*例:動物の中の猫、犬
*猫というオブジェクトに対応するタイプは猫タイプです
*猫x=new猫();
*同時に猫も動物の一種で、猫を動物と呼ぶこともできます
*動物y=new猫();
*動物は、猫や犬の具体的なものから抽出される親のタイプです
*親タイプ参照は子オブジェクトを指します
体現する
*親またはインタフェースの参照は、自分の子オブジェクトを指したり受け入れたりします.
さぎょう
*マルチステートの存在により、プログラムの拡張性とポスト・メンテナンス性が向上
前提条件
*継承または実装の関係が必要
例(関係があるかどうかを確認)
class A extends B{}
B b = new A();
If(b instanceof A){ ...
}
*上書き操作が必要
分類:
*コンパイル時のマルチステート:メソッドの再ロード
*ランタイムマルチステート:メソッドの上書き
マルチステートの特徴
*メンバー関数
*コンパイル時:参照変数が属するクラスで呼び出されたメンバーがあるかどうかを確認します.
*実行時:オブジェクトが属するクラスで呼び出されたメンバーがあるかどうかを確認します.
コンパイル時のタイプは、その変数を宣言する際に使用されるタイプによって決定され、実行時のタイプは、実際に変数に割り当てられたオブジェクトによって決定されます.
コンパイル時タイプと実行時タイプが異なる場合、マルチステートが表示されます.
メンバー変数
*参照変数が属するクラスのみ
参照変数タイプ変換{{さんしょうへんすう:がたへんかん}}
アップシフト(子→親):(自動完了)
親名親オブジェクト=子インスタンス
ダウンシフト(親→子):(強制完了)
子クラス名は子クラスオブジェクト=(子クラス名)親クラスインスタンスと呼ばれます.
オブジェクト名instanceofクラス
指定した変数名がこのとき参照される真のタイプが現在与えられているクラスまたはサブクラスであるかどうかを判断します.
コンピュータの実行例
Obejecクラス-equals()、Objectクラス-toString()
すべてのクラスの共通の親クラスは、1つのクラスが表示されずに1つのクラスを継承すると、その直接の親クラスはObjectに違いありません.
すべてのデータ型はObjectで受信できます
class OOXX extends Object{}はclass ooXX{}に等しい
一般的な方法
public boolean equals(Object obj):オブジェクト比較
public int hashCode():当該対象のHashコードを取得する
public String toString()public String toString()オブジェクトの説明
ObjectクラスのtoString()メソッド:「オブジェクトの説明」
すべてのクラスにこのメソッドを上書きすることを推奨します.
出力オブジェクトを直接印刷すると、そのオブジェクトのtoString()メソッドが呼び出されます.//書かなくてもいいです
オブジェクトを印刷するとき、実際に呼び出されたオブジェクトが実際に指すクラスの自己記述.
全限定クラス名+@+16進数のhashCode値は、
全限定クラス名+@+IntegertoHexString(このオブジェクト.hashCode)
equalsも同じオブジェクトを指すかどうかを判断する
実際の意味がないので書き直す必要がある
public boolean equals(Object obj) {}
StringはObjectのequalsメソッドを上書きしました:文字のシーケンスが同じかどうかを比較します
==2つの変数が等しいかどうかを判断する
基本タイプ:
参照タイプ:trueを指定するには、同じオブジェクトを指定する必要があります.
親子または平レベルの関係を持つ2つのオブジェクトnewString("1")==new String("1");?
マルチステート
定義:ある種類の事物が存在する多種の存在形態.
*例:動物の中の猫、犬
*猫というオブジェクトに対応するタイプは猫タイプです
*猫x=new猫();
*同時に猫も動物の一種で、猫を動物と呼ぶこともできます
*動物y=new猫();
*動物は、猫や犬の具体的なものから抽出される親のタイプです
*親タイプ参照は子オブジェクトを指します
体現する
*親またはインタフェースの参照は、自分の子オブジェクトを指したり受け入れたりします.
さぎょう
*マルチステートの存在により、プログラムの拡張性とポスト・メンテナンス性が向上
前提条件
*継承または実装の関係が必要
例(関係があるかどうかを確認)
class A extends B{}
B b = new A();
If(b instanceof A){ ...
}
*上書き操作が必要
分類:
*コンパイル時のマルチステート:メソッドの再ロード
*ランタイムマルチステート:メソッドの上書き
/*
: 。
: ,
: , 。
x = new ();
x = new ();
1,
。
。
2,
。 , 。
: 。
3,
。
4, :
, 。
5,
*/
/*
,
, 。
*/
abstract class Animal
{
abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println(" ");
}
public void catchMouse()
{
System.out.println(" ");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println(" ");
}
public void kanJia()
{
System.out.println(" ");
}
}
class Pig extends Animal
{
public void eat()
{
System.out.println(" ");
}
public void gongDi()
{
System.out.println(" ");
}
}
//-----------------------------------------
class DuoTaiDemo
{
public static void main(String[] args)
{
//Cat c = new Cat();
//c.eat();
//Dog d = new Dog();
//d.eat();
//Cat c = new Cat();
/*
Cat c1 = new Cat();
function(c1);
function(new Dog());
function(new Pig());
*/
//Animal c = new Cat();
//c.eat();
function(new Cat());
function(new Dog());
function(new Pig());
}
public static void function(Animal a)//Animal a = new Cat();
{
a.eat();
//a.catchMouse();
}
/*
public static void function(Cat c)//
{
c.eat();
}
public static void function(Dog d)
{
d.eat();
}
public static void function(Pig p)
{
p.eat();
}
*/
}
マルチステートの特徴
*メンバー関数
*コンパイル時:参照変数が属するクラスで呼び出されたメンバーがあるかどうかを確認します.
*実行時:オブジェクトが属するクラスで呼び出されたメンバーがあるかどうかを確認します.
コンパイル時のタイプは、その変数を宣言する際に使用されるタイプによって決定され、実行時のタイプは、実際に変数に割り当てられたオブジェクトによって決定されます.
コンパイル時タイプと実行時タイプが異なる場合、マルチステートが表示されます.
メンバー変数
*参照変数が属するクラスのみ
参照変数タイプ変換{{さんしょうへんすう:がたへんかん}}
アップシフト(子→親):(自動完了)
親名親オブジェクト=子インスタンス
ダウンシフト(親→子):(強制完了)
子クラス名は子クラスオブジェクト=(子クラス名)親クラスインスタンスと呼ばれます.
オブジェクト名instanceofクラス
指定した変数名がこのとき参照される真のタイプが現在与えられているクラスまたはサブクラスであるかどうかを判断します.
/*
, ( )
: 。
*/
/*
,
, 。
*/
class Cat extends Animal
{
public void eat()
{
System.out.println(" ");
}
public void catchMouse()
{
System.out.println(" ");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println(" ");
}
public void kanJia()
{
System.out.println(" ");
}
}
class Pig extends Animal
{
public void eat()
{
System.out.println(" ");
}
public void gongDi()
{
System.out.println(" ");
}
}
//-----------------------------------------
class DuoTaiDemo2
{
public static void main(String[] args)
{
//Animal a = new Cat();// 。 。
//a.eat();
// , ?
// 。 。 。
///Cat c = (Cat)a;
//c.catchMouse();
// , 。
// , , 。
// 。
// Animal a = new Animal();
// Cat c = (Cat)a;
/*
x = new ();
x. ();
y = ( )x;
y. ();
*/
function(new Dog());
function(new Cat());
}
public static void function(Animal a)//Animal a = new Cat();
{
a.eat();
/*
if(a instanceof Animal)
{
System.out.println("haha");
}
else
*/
if(a instanceof Cat)
{
Cat c = (Cat)a;
c.catchMouse();
}
else if(a instanceof Dog)
{
Dog c = (Dog)a;
c.kanJia();
}
/*
instanceof : 。 intanceof ( )
*/
}
}
class Fu
{
static int num = 5;
void method1()
{
System.out.println("fu method_1");
}
void method2()
{
System.out.println("fu method_2");
}
static void method4()
{
System.out.println("fu method_4");
}
}
class Zi extends Fu
{
static int num = 8;
void method1()
{
System.out.println("zi method_1");
}
void method3()
{
System.out.println("zi method_3");
}
static void method4()
{
System.out.println("zi method_4");
}
}
class DuoTaiDemo4
{
public static void main(String[] args)
{
// Fu f = new Zi();
//
// System.out.println(f.num);
//
// Zi z = new Zi();
// System.out.println(z.num);
//f.method1();
//f.method2();
//f.method3();
Fu f = new Zi();
System.out.println(f.num);
f.method4();
Zi z = new Zi();
z.method4();
/*
:
: 。 , , 。
: 。
: , , 。
, :
, ( )。
, :
, 。
*/
// Zi z = new Zi();
// z.method1();
// z.method2();
// z.method3();
}
}
コンピュータの実行例
/*
:
,
。
*/
interface PCI
{
public void open();
public void close();
}
class MainBoard
{
public void run()
{
System.out.println("mainboard run ");
}
public void usePCI(PCI p)//PCI p = new NetCard()// 。
{
if(p!=null)
{
p.open();
p.close();
}
}
}
class NetCard implements PCI
{
public void open()
{
System.out.println("netcard open");
}
public void close()
{
System.out.println("netcard close");
method();
}
}
class SoundCard implements PCI
{
public void open()
{
System.out.println("SoundCard open");
}
public void close()
{
System.out.println("SoundCard close");
}
}
/*
class MainBoard
{
public void run()
{
System.out.println("mainboard run");
}
public void useNetCard(NetCard c)
{
c.open();
c.close();
}
}
class NetCard
{
public void open()
{
System.out.println("netcard open");
}
public void close()
{
System.out.println("netcard close");
}
}
*/
class DuoTaiDemo5
{
public static void main(String[] args)
{
MainBoard mb = new MainBoard();
mb.run();
mb.usePCI(null);
mb.usePCI(new NetCard());
mb.usePCI(new SoundCard());
}
}
Obejecクラス-equals()、Objectクラス-toString()
すべてのクラスの共通の親クラスは、1つのクラスが表示されずに1つのクラスを継承すると、その直接の親クラスはObjectに違いありません.
すべてのデータ型はObjectで受信できます
class OOXX extends Object{}はclass ooXX{}に等しい
一般的な方法
public boolean equals(Object obj):オブジェクト比較
public int hashCode():当該対象のHashコードを取得する
public String toString()public String toString()オブジェクトの説明
ObjectクラスのtoString()メソッド:「オブジェクトの説明」
すべてのクラスにこのメソッドを上書きすることを推奨します.
出力オブジェクトを直接印刷すると、そのオブジェクトのtoString()メソッドが呼び出されます.//書かなくてもいいです
オブジェクトを印刷するとき、実際に呼び出されたオブジェクトが実際に指すクラスの自己記述.
全限定クラス名+@+16進数のhashCode値は、
全限定クラス名+@+IntegertoHexString(このオブジェクト.hashCode)
equalsも同じオブジェクトを指すかどうかを判断する
実際の意味がないので書き直す必要がある
public boolean equals(Object obj) {}
StringはObjectのequalsメソッドを上書きしました:文字のシーケンスが同じかどうかを比較します
==2つの変数が等しいかどうかを判断する
基本タイプ:
参照タイプ:trueを指定するには、同じオブジェクトを指定する必要があります.
親子または平レベルの関係を持つ2つのオブジェクトnewString("1")==new String("1");?
/*
Object: , 。
。
Object 。
, 。
, 。 。
*/
class Demo //extends Object
{
private int num;
Demo(int num)
{
this.num = num;
}
public boolean equals(Object obj)//Object obj = new Demo();
{
if(!(obj instanceof Demo))
return false;
Demo d = (Demo)obj;
return this.num == d.num;
}
/*
public boolean compare(Demo d)
{
return this.num==d.num;
}
*/
public String toString()
{
return "demo:"+num;
}
}
class Person
{
}
class ObjectDemo
{
public static void main(String[] args)
{
Demo d1 = new Demo(4);
System.out.println(d1);// , toString 。 。
Demo d2 = new Demo(7);
System.out.println(d2.toString());
//Demo d2 = new Demo(5);
//Class c = d1.getClass();
//
// System.out.println(c.getName());
// System.out.println(c.getName()+"@@"+Integer.toHexString(d1.hashCode()));
// System.out.println(d1.toString());
//Person p = new Person();
///System.out.println(d1.equals(p));
}
}