[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 ...
随机推荐
- Spring的JDBC Template
Spring的JDBC Template(JDBC模板)简化JDBC API开发,使用上和Apache公司的DBUtils框架非常类似) 快速入门实例 1.创建项目后,导入Spring基础核心开发包. ...
- LOJ2319. 「NOIP2017」列队【线段树】
LINK 思路 神仙线段树 你考虑怎么样才能快速维护出答案 首先看看一条链怎么做? 首先很显然的思路是维护每个节点的是否出过队 然后对于重新入队的点 直接在后面暴力vector存一下就可以了 最核心的 ...
- 第01章-欢迎学习VTK
[译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年,ISBN: 978-1-930934- ...
- Tomcat 7 可以修改 Session 默认的 Cookie 名 JSESSIONID 了
Tomcat 7 可以修改 Session 默认的 Cookie 名 JSESSIONID 了 程序员必上的开发者服务平台 —— DevStore 看看下面这个配置: <Contex ...
- CentOS7.2部署FTP
目前Linux大部分部署的FTP服务器都是vsftpd,至于为什么,暂时没什么必要深究. 1.安装vsftpd # yum check-update //检查可更新的程序,也可以不更新直接安装,以防万 ...
- eclipse 的安装和汉化
第一步:直接百度搜索eclipse,第一个就是官方网站 第二步:点击DOWNLOAD 64BIT进入下载页面 第三步:点击DOWNLOAD进行下载 If the download doesn't st ...
- Visual Studio环境变量、工作目录、vc++目录、 命令等 的配置和作用
在调试 Visual Studio 2008 程序时,经常有一些动态链接库(即 dll 文件)需要加载到工程里,这样才能依赖第三方库进行程序调试. 这些动态链接库,往往都是测试版本或是开发中的版本,或 ...
- FatJar—Java Application制作jar包
Fat Jar功能非常强大,先下载吧. 下载地址:sourceforge.net/projects/fjep/files/ http://files.cnblogs.com/files/nidongd ...
- MTU&MSS
MTU是Maximum Transmission Unit的缩写,意为最大传输单元,通俗的理解就是在网络上传送的最大数据包,单位是字节. 以太网对数据帧的长度都有一个限制,其最大值为1500,这个特性 ...
- javascript使用bind指定接收者
var json = { jArray: [], jPush: function (c) { this.jArray.push(c); } } var examp = ["123" ...