[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 ...
随机推荐
- 【idea】如何破解idea
1.IntelliJ IDEA官网下载 https://www.jetbrains.com/idea/download/ 2.安装IntelliJ IDEA 3.永久破解 在http://idea.l ...
- 在WPF中使用CefSharp嵌入浏览器(转)
在WPF中使用CefSharp嵌入浏览器 日常开发中,我们需要将一些Web页面嵌入到桌面客户端软件中.下面我们使用CefSharp嵌入浏览器来实现. 首先先介绍一下CefSharp嵌入式浏览器,它 ...
- bzoj1925(SCOI2010)地精部落
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1925 要怎样才能想出正解呢? 当然有一维表示从1到 i . 发现最后是递增的方案数=最后是递 ...
- Linux环境安装jdk10
一. 下载jdk 下载方式一:直接在linux上下载 wget --no-check-certificate --no-cookies --header "Cookie: oraclelic ...
- SlickEdit V21 2016 破解教程,win linux mac
最近主要工作系统转到LInux上面来了,Slickedit的安装破解也费了些事,今天将过程整理一下做个记录. 说明:SlickEdit pro V21.03 Linux 64位实测可用,MAC实测可用 ...
- mysql 计算经纬度函数(米)
) CHARSET utf8mb4 begin return ROUND( * ASIN( SQRT( POW( SIN( ( lat1 ) ), ) ) ) * POW( SIN( ( lon1 ) ...
- 【整理总结】目录 - 代码沉淀 - 常见Nuget包介绍及使用
为了团队项目或者后期代码维护方便,现在项目已经极致推荐使用Nuget管理第三方类库了,所以下面列举的类库,都会在Nuget上获取到.目录排序不分先后,如果查找困难,请使用 ctrl + F 重要提示: ...
- python数据库连接池基于DBUtils
DBUtils模块的使用的两种方式 DBUtils是Python的一个用于实现数据库连接池的模块 安装 pip install DBUtils 1.使用姿势一(不建议此方法) 为每个线程 (资源占用过 ...
- 告诉你C盘里的每个文件夹都是干什么用的 ! ! !
Documents and Settings是什么文件? 答案: 是系统用户设置文件夹,包括各个用户的文档.收藏夹.上网浏览信息.配置文件等. 补:这里面的东西不要随便删除,这保存着所有用户的文档 ...
- python实现socket上传下载文件-进度条显示
在python的socket编程中,可以实现上传下载文件,并且在下载的时候,显示进度条,具体的流程如下图所示: 1. 服务器端代码如下: [root@python 519]# cat server.p ...