博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java子类对象和成员变量的隐写&方法重写
阅读量:6983 次
发布时间:2019-06-27

本文共 3923 字,大约阅读时间需要 13 分钟。

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()不会产生生活效果。

转载于:https://www.cnblogs.com/xinxianquan/p/8729238.html

你可能感兴趣的文章
C#获取文件的MD5值
查看>>
字符串子串查找strstr
查看>>
个人Vue-1.0学习笔记
查看>>
string的属性小试
查看>>
JS中3种弹出窗口函数区别分析
查看>>
《深入理解计算机系统》 优化程序性能的几个方法
查看>>
WPF项目中使用水晶报表for vs2010时的一个找不到程序集的问题
查看>>
关于C/C++的一些思考(2)
查看>>
hive计算周一的日期
查看>>
邻接表——最简单易懂的写法——向非我非非我大佬低头
查看>>
需求获取常见的方法是进行客户访谈,结合你的实践谈谈会遇到什么问题,你是怎么解决的?...
查看>>
windows programming
查看>>
a标签的link、visited、hover、active的顺序
查看>>
SQL系列(四)—— 唯一值(distinct)
查看>>
《数据结构》例1.3
查看>>
堆和栈的区别 (转贴)
查看>>
OpenSSL s_server / s_client 应用实例
查看>>
微信小程序开发工具(0.9.092300)下载地址,分享给没有公众号的小伙伴
查看>>
项目中的常见算法
查看>>
(转)GCT之逻辑经验总结(拿来主义)
查看>>