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()); } } |
运行结果:

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){
}
}
}
}
}
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();
}
}
}
java子类对象和成员变量的隐写&方法重写的更多相关文章
- java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。
//------------------------------------------------我是代码的分割线 // 首选是一个工具类,在该工具类里面,定义了一个方法,public void s ...
- Java 访问限制符 在同一包中或在不同包中:使用类创建对象的权限 & 对象访问成员变量与方法的权限 & 继承的权限 & 深入理解protected权限
一.实例成员与类成员 1. 当类的字节码被加载到内存, 类中类变量.类方法即被分配了相应内存空间.入口地址(所有对象共享). 2. 当该类创建对象后,类中实例变量被分配内存(不同对象的实例变量互不相同 ...
- 假如java类里的成员变量是自身的对象
假如java类里的成员变量是自身的对象,则新建该类对象时内存中怎么分配空间,我感觉似乎死循环了. 不过我想的肯定是错的,因为很多类的成员变量是自身对象,并且绝对无错,举个例子: Class A{ pr ...
- Java学习日记基础篇(四)——类,对象之成员变量,成员方法,构造方法
面向对象(Object Oriented) 一.面向对象杂谈 面向对象(Object Oriented),我的翻译是以物体为目标的,就是说编程的时候是建立一个物体,然后对这个物体进行操作. Java语 ...
- java类里的成员变量是自身的对象问题
今晚看单例模式饿汉时想到一个问题:假如java类里的成员变量是自身的对象,则新建该类对象时内存中怎么分配空间,我感觉似乎死循环了.于是上网搜索了下,哈哈,果然有人早就思考过这个问题了,站在巨人的肩膀上 ...
- java基础疑难点总结之成员变量的继承,方法重载与重写的区别,多态与动态绑定
1.成员变量的继承 1.1要点 子类用extends关键字继承父类.子类中可以提供新的方法覆盖父类中的方法.子类中的方法不能直接访问父类中的私有域,子类可以用super关键字调用父类中的方法.在子类中 ...
- JAVA中局部变量 和 成员变量有哪些区别
JAVA中局部变量 和 成员变量有哪些区别 1.定义的位置不一样<重点>***局部变量:在方法的内部成员变量:在方法的外部,直接写在类当中 2.作用范围不一样<重点>***局部 ...
- 只用@property定义一个属性speed,子类不能直接用_speed,需要在interface的成员变量列表里写上_speed
//写法一: @interface Person : NSObject { } @property (nonatomic, strong) NSString *name; @end @implemen ...
- java笔记13之成员变量与局部变量
成员变量和局部变量的区别 1在类中的位置不同 局部变量:类的方法体内 成员变量:类的方法之外 2内存的不同位置 局部变量:在栈内存中 成员位置:在堆内存 3生命周期不同 局部变量:随着方法的调用而存在 ...
随机推荐
- MongoDB 学习手册 - CURD
mongoDB 增加数据 // mongoDB 增加数据: //新增数据insert( 字典 ) 表示插入一条数据,insert([字典]) 表示插入多条数据 // db.text01.insert( ...
- PF部分代码解读
// 单个粒子数据结构 typedef struct { // 粒子状态 pf_vector_t pose; // 粒子权重 double weight; } pf_sample_t; // Info ...
- vue中过滤器filters的使用
组件内写法 filters:{ filter:function(data,arg1,arg2){ return .... } } 全局写法 filters('filter',function(data ...
- MySQL常用的sql操作
1.日期时间格式化 2.日期时间格式化 3.查询第11到第15条数据 ,5//落过多少,取出多少. 4.字符串转日期 select str_to_date('2016-01-02', '%Y-%m-% ...
- 扒一扒JVM的垃圾回收机制,下次面试你准备好了吗
相信和小编一样的程序猿们在日常工作或面试当中经常会遇到JVM的垃圾回收问题,有没有在夜深人静的时候详细捋一捋JVM垃圾回收机制中的知识点呢?没时间捋也没关系,因为小编接下来会给你捋一捋. 一. 技 ...
- Path Sum I && II & III
Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ad ...
- Saltstack自动化操作记录(1)-环境部署
早期运维工作中用过稍微复杂的Puppet,下面介绍下更为简单实用的Saltstack自动化运维的使用. Saltstack知多少Saltstack是一种全新的基础设施管理方式,是一个服务器基础架构集中 ...
- python下载夏目友人帳
python下载夏目友人帐 一般情况下我们使用爬虫更多的应该是爬数据或者图片吧,今天在这里和大家分享一下关于使用爬虫技术来进行视频下载的方法,不仅可以方便的下载一些体积小的视频,针对大容量的视频下载同 ...
- 设计模式C++学习笔记之十三(Decorator装饰模式)
装饰模式,动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活. 13.1.解释 main(),老爸 ISchoolReport,成绩单接口 CFourt ...
- vc++高级班之多线程篇[6]---线程间的同步机制①
①.线程同步的必要性: int g_Num = 0; UINT __cdecl ThreadProc(LPVOID lpParameter) { for (int idx = 0; idx &l ...