[Training Video - 3] [Java Introduction] [Object Oriented Programming]
Static and non-Static :
非静态方法(不带static)可以访问静态变量和方法(带static),但是反过来就不行。
类的静态成员(变量和方法)属于类本身,在类加载的时候就会分配内存,可以通过类名直接去访问。
非静态成员(变量和方法)属于类的对象,只有在类的对象产生(创建实例)的时候才会分配内存,然后通过类的对象去访问。
在一个类的静态成员中去访问非静态成员之所以会出错是因为在类的非静态成员不存在的时候静态成员就已经存在了,访问一个内存中不存在的东西会出错。

静态变量和方法既可以用类来调用,也可以用对象来调用,但不建议用对象调用
Code :
public class Car {
// GLOBAL VARIABLE
String mod;
int price;
static int wheels = 4;
// static functions can only access static stuff
public static void main(String[] args) {
Car c1 = new Car();
c1.mod = "Merc";
c1.price = 8909809;
c1.start();
c1.accel();
Car c2 = new Car();
c2.mod = "Maruti";
c2.price = 8909809;
c2.start();
c2.accel();
System.out.println(c1.mod);
System.out.println(c2.mod);
// static
System.out.println(wheels);
System.out.println(Car.wheels);
c1.wheels = 8;
System.out.println(wheels);
System.out.println(Car.wheels);
System.out.println(c2.wheels);
fillGas(100);
Car.fillGas(200);
c1.fillGas(900);
}
public void start(){
System.out.println(mod + " starting");
}
public void accel(){
System.out.println(mod + " acc");
}
public static void fillGas(int quantity){
}
}
Result :
Merc starting
Merc acc
Maruti starting
Maruti acc
Merc
Maruti
4
4
8
8
8
Code :
public class Car {
String mod;
public static void main(String[] args) {
Car a = new Car();
Car b = new Car();
Car c = new Car();
a.mod = "A";
b.mod = "B";
c.mod = "C";
System.out.println(a.mod);
System.out.println(b.mod);
System.out.println(c.mod);
a=b;
b=c;
c=a;
System.out.println(a.mod);
System.out.println(b.mod);
System.out.println(c.mod);
}
}
Result :
A
B
C
B
C
B
[Training Video - 3] [Java Introduction] [Object Oriented Programming]的更多相关文章
- [Training Video - 2] [Java Introduction] [Operator, Loops, Arrays, Functions]
Operator : While Loop : For Loop : Arrays : Code : public class FirstJavaClass { public static void ...
- [Training Video - 2] [Java Introduction] [Install Java and Eclipse, Create Class]
Download Java : https://java.com/en/download/ Download Eclipse : https://www.eclipse.org/downloads/ ...
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- JavaScript: Constructor and Object Oriented Programming
Constructor : Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...
- 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性
一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...
- Java - 面向对象(object oriented)计划 详细解释
面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...
- python, 面向对象编程Object Oriented Programming(OOP)
把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数 ...
- Python 面向導向語言 Object Oriented Programming Language
Pytho 是面向對象的程式語言,舉凡 Literals 值都是 Object.例如: >>> id(38)8791423739696 與 >>> id('ABC' ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
随机推荐
- 解决Apache下生成静态页面乱码的问题
我的空间存放在阿里云,服务器默认Apache编码设置为utf-8,而新的网站珠宝招聘网http://hr.izuans.com 采用GB2312编码,其他程序文件都OK,就是生成静态新闻页和其他单页面 ...
- 实用的IP地址处理模块IPy
https://www.cnblogs.com/cherishry/p/5916935.html IPy安装 pip install IPy IP地址.网段的基本处理 IPy模块包含IP类,使用它可以 ...
- WCF揭秘学习笔记(1):基础知识
最近找工作,面试时经常被问懂不懂WCF.不少招聘高级.NET工程师的要求上都 写着有WCF开发经验的优先考虑.我对于WCF仅仅是通过看一些教学视频这种山寨学习法了解一些.现在要下决心好好学习一下WCF ...
- Poly
folly/Poly.h Poly is a class template that makes it relatively easy to define a type-erasing polymor ...
- <转>cocos2d-x学习笔记(五)仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
转载自ufolr的博客 原文连接:http://blog.csdn.net/ufolr/article/details/7624851 最近项目中需要一个落叶的效果,本来想用粒子特效来实现,但是几经调 ...
- 网页中显示pdf的方法
非常好的在网页中显示pdf的方法 今天有一需求,要在网页中显示pdf,于是立马开始搜索解决方案,无意中发现一个非常好的解决方法,详见http://blogs.adobe.com/pdfdevjunki ...
- centos7上安装rar解压软件
(http://www.rarlab.com)官网可以查看最新的版本 wget https://www.rarlab.com/rar/rarlinux-x64-5.5.0.tar.gz 2.tar.g ...
- Input设置只读属性
input设置为只读一般用于查看详情: 格式为: (1)单纯的input框 <div class="form-group"> <label for="c ...
- Git 知识总结
版本控制git之一 - 仓库管理 版本控制git之二-分支 git https://mp.weixin.qq.com/s/96FS12DTzbjAJQ1ynRNqdg git init 初始化目录 g ...
- Tomcat 性能监控工具jvisualvm, JConsole
配置: 重启Tomcat