java子類對象和成員變量的隱寫&方法重寫


1、子類繼承的方法只能操作子類繼承和隱藏的成員變量名字類新定義的方法可以操作子類繼承和子類新生命的成員變量,但是無法操作子類隱藏的成員變量(需要適用super關鍵字操作子類隱藏的成員變量。)

public class ChengYuanBianLing {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CheapGoods cheap=new CheapGoods();
//        cheap.weight=192.32;//非法
        cheap.newSetWeight(23);
        System.out.println(cheap.weight);
        System.out.println(cheap.newGetPrice());
        cheap.oldSetWight(3.32);
        System.out.println(cheap.oldGetPrice());

    }

}
class Goods{
    public double weight;
    public void oldSetWight(double w){
        weight=w;
        System.out.println("double型de weight="+weight);
    }
    public double oldGetPrice(){
        double price=weight*10;
        return price;
    }
    
}
class CheapGoods extends Goods{
    public int weight;
    public void newSetWeight(int w){
        weight=w;
        System.out.println("新的weight="+weight);
    }
    public double newGetPrice(){
        double price=weight*10;
        return price;
    }
    
}

2、方法重寫 override method override

   方法重寫就是子類繼承父類,子類方法中使用相同的方法名字和參數個數以及參數類型。子類通過重寫父類的方法,可以隱藏父類的方法,重寫父類的狀態和行為改變為自己的狀態和行為。

1、java線程就是一個object類,其實例繼承類java.lang.Thread或其子類,創建編寫線程運行時執行的代碼有兩種方式,一種是創建Thread子類的一個實例並重寫run方法,一種是創建類的時候實現幾口Runnable接口,如下展示的是run和start的區別。通過 調用start就會創建一個新的線程,但是run方法不會。run方法只會在當前的線程中運行。跟普通方法沒有任何區別。

 

public class Test {
     public static void main(String[] args)  {
         System.out.println( "主線程ID:" +Thread.currentThread().getId());
         MyThread thread1 = new MyThread( "thread1" );
         thread1.start();
         MyThread thread2 = new MyThread( "thread2" );
         thread2.run();
     }
}
 
class MyThread extends Thread{
     private String name;
 
     public MyThread(String name){
         this .name = name;
     }
 
     @Override
     public void run() {
         System.out.println( "name:" +name+ " 子線程ID:" +Thread.currentThread().getId());
     }
}

運行結果:

 
2、線程同步問題,
public class XianChengTongBuWenTi {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Bank bank=new Bank();
        bank.setMoney(300);
        Thread account, chsher;
        account=new Thread(bank);
        chsher=new Thread(bank);
        account.setName("讀者");
        chsher.setName("寫者");
        account.start();
        chsher.start();

    }

}
class Bank implements Runnable{
    int money=200;
    public void setMoney(int n){
        money=n;
    }
    public void run(){
        if(Thread.currentThread().getName().equals("讀者"))
            saveOrTake(200);
        else if(Thread.currentThread().getName().equals("寫者"))
        saveOrTake(300);
    }
    public synchronized void saveOrTake(int amount){
        if(Thread.currentThread().getName().equals("讀者")){
            
//        while(true){
        for(int i=1;i<=1;i++){
            money+=amount/3;
            System.out.println(Thread.currentThread().getName()+"開始工作"+"有這么多字"+amount);
            try{
                Thread.sleep(1000);
            }catch(InterruptedException ex){
                
            }
        }
        }else if(Thread.currentThread().getName().equals("寫者")){
            for(int i=1;i<=1;i++){
                money+=amount/3;
                System.out.println(Thread.currentThread().getName()+"開始工作"+"有這么多字"+amount);
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException ex){
                    
                }
        }
    }
    
}
}
3、協調線程同步問題 ,wait和notify以及notifyall是object類中的final方法,被所有的類繼承且不允許重寫的方法,不可以在非同步方法中使用這些關鍵字,
public class XieTiaoTongBu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TicketHouse officer=new TicketHouse();
        Thread Tian, Ya;
        Tian=new Thread(officer);
        Tian.setName("田亞明");
        Ya=new Thread(officer);
        Ya.setName("倩倩");
        Tian.start();
        Ya.start();
    }
}
class TicketHouse implements Runnable{
    int fiveAmount=2,tenAmount=0,twentyAmount=0;
    public void run(){
        if(Thread.currentThread().getName().equals("田亞明")){
            saleTicket(20);
        }
        else if(Thread.currentThread().getName().equals("倩倩")){
            saleTicket(29);
        }
    }
    private synchronized void saleTicket(int money){
        if(money==20){
            fiveAmount+=1;
            System.out.println(Thread.currentThread().getName()+"真好合適");
        }
        else if(money==29){
            while(fiveAmount<=3){
                try{
                    System.out.println("\n"+Thread.currentThread().getName()+"等待");
                    wait();
                }catch(InterruptedException ex){
                    
                }
                fiveAmount-=3;
                twentyAmount-=1;
                System.out.println(Thread.currentThread().getName());
            }
            notifyAll();
        }
    }
}
4、線程聯合問題 ,假設線程A在運行期間聯合線程B,那么A線程會立刻終止,一直到B線程執行完畢之后A線程才會再一次排隊等候CPU資源,以便恢復運行,但是如果A准備連聯合的B線程已經結束,那么B.join()不會產生生活效果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM