文章目录
1、面向对象概述
2、类与对象
package class_and_object;
public class Class1 {
int sum() {
int i = 0;
for(i = 0; i < 10; i++) {
System.out.println(i);
if(i == 3) {
return -1;
}
}
return i;
}
public static void main(String[] args) {
// demo
Class1 c = new Class1();
int a = c.sum();
System.out.println(a);
}
}
package class_and_object;
public class Class2 {
int sum(int...x) {
for(int i = 0; i < x.length; i++) { //将传入的不定长参数 自动转换成一个数组来看
System.out.println(x[i]);
}
return 0;
}
public static void main(String[] args) {
// 参数的种类:不定长参数
Class2 c = new Class2();
int a = c.sum(56, 78, 99, 32);
System.out.println(a);
}
}
3、构造方法
你不允许创建,而我可以自己来创建我自己
4、局部变量
5、 对象的使用与创建
我们这里有一个Test类,声明一个变量,然后直接等于我这个类的构造方法就可以了,这样我们创建出的这个变量就是类的对象了
package class_and_object;
public class TransferProperty {
int i = 47;
public void call() {
System.out.println("调用call()方法");
for(int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
// 创建TransferProperty类,在该类中说明 对象是如何调用类成员的
TransferProperty t1 = new TransferProperty();
System.out.println("第一个实例对象调用变量i的结果:" + t1.i);
TransferProperty t2 = new TransferProperty();
t2.i = 60; //把成员变量的值改了
System.out.println("第二个实例对象调用变量i的结果:" + t2.i);
t1.call();
t2.call();
}
}
package class_and_object;
public class TransferProperty {
int i = 47;
public void call() {
System.out.println("调用call()方法");
if(i == 47) {
System.out.println("成员变量i是默认值");
}
else {
System.out.println("成员变量i不是默认值");
}
}
public static void main(String[] args) {
// 创建TransferProperty类,在该类中说明 对象是如何调用类成员的
TransferProperty t1 = new TransferProperty();
System.out.println("第一个实例对象调用变量i的结果:" + t1.i);
TransferProperty t2 = new TransferProperty();
t2.i = 60; //把成员变量的值改了
System.out.println("第二个实例对象调用变量i的结果:" + t2.i);
t1.call();
t2.call();
}
}
6、 对象的销毁
每个对象都有生命周期,当对象的生命周期结束时,分配给该对象的内存地址将会被回收,
在其他语言中需要手动回收这些废弃的对象,而java有一套完整的垃圾回收机制
虽然垃圾回收机制已经很完善了,但是垃圾回收器只能够回收那些由new操作符创建的对象。如果某些对象不是通过new操作符创建的,可能不能被垃圾回收器所识别,所以java提供了一个finalize()方法
这个方法是object类提供的方法,它被声明是可继承的,用户可以在自己的类中重新定义这个方法。在垃圾回收时,会首先调用该方法,并且在下一次垃圾回收动作发生时,才会真正回收这个对象所占用的内存。
由于这垃圾回收不受人为控制,所以它执行的时间也不确定,我们这个finalize()方法也就无法执行
所以java提供System.gc()的方法,来强制启动垃圾回收器
package class_and_object;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test = new Test();
test = null;
System.gc(); //强制启动垃圾回收器
}
}
package class_and_object;
public class Test {
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
System.out.println("调用finalize()方法");
}
}
7、this关键字
package class_and_object;
public class Book {
String name = "abc";
public void showName(String name) {
System.out.println(name);
System.out.println(this.name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Book b = new Book();
b.showName("123");
}
}
package class_and_object;
public class EggCake {
int eggCount;
public EggCake(int eggCount) {
this.eggCount = eggCount;
System.out.println("鸡蛋灌饼里有" + this.eggCount + "个鸡蛋");
}
//代码重复性太高
// public EggCake() {
// this.eggCount = 1;
// System.out.println("鸡蛋灌饼里有" + this.eggCount + "个鸡蛋");
// }
public EggCake() {
this(1); //调用有参数的构造方法
int i = 1; //这句话只能放在这,而不能放在前面,你在构造方法运行之前,不可以做任何初始化操作,
//因为构造方法本身就是初始化,你在初始化操作之前还要做初始化的话,这是不符合逻辑的
}
public static void main(String[] args) {
EggCake e1 = new EggCake(5);
EggCake e2 = new EggCake();
}
}
8、static关键字
(一)
内存中静态区的变量是可以被共享的,其他类调用本类的静态变量和静态方法时,无需实例化就可以调用
静态区就是内存的公共区,我们要使用一个非静态的方法的话,需要创建这个类的对象才能使用, 这个过程就相当于先把这个类找到了才能使用
但很多时候,不同的类之间需要对同一个变量进行操作
package class_and_object;
public class Pool {
static public int water = 0;
public void out() { //出水
if(water >= 2) {
water -= 2;
}
else {
water = 0;
}
}
public void in() { //进水
water += 3;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Pool out = new Pool();
Pool in = new Pool();
// System.out.println("水池的水量:" + in.water);
// System.out.println("水池的水量:" + out.water);
System.out.println("水池的水量:" + Pool.water);
}
这个water是静态成员,静态的成员归类所有,而不是归对象所有,所以这种调用方式不正规
静态成员可以直接用类.的方式来调用
package class_and_object;
public class Pool {
static public int water = 0;
public void out() { //出水
if(water >= 2) {
water -= 2;
}
else {
water = 0;
}
}
public void in() { //进水
water += 3;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Pool out = new Pool();
Pool in = new Pool();
System.out.println("水池的水量:" + Pool.water);
System.out.println("注水两次");
in.in();
in.in();
System.out.println("水池的水量:" + Pool.water);
System.out.println("放水一次");
out.out();
System.out.println("水池的水量:" + Pool.water);
System.out.println("放水三次");
out.out();
out.out();
out.out();
System.out.println("水池的水量:" + Pool.water);
}
}
两个完全独立的对象,他们在同时操作同一个属性(变量)时,这个变量必须是静态的
(二)静态变量和静态常量
package class_and_object;
public class Static_Variable {
static int x;
int y;
public Static_Variable(int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Static_Variable a = new Static_Variable(1, 2);
Static_Variable b = new Static_Variable(13, 17);
System.out.println("a.x = " + a.x);
System.out.println("a.y = " + a.y);
System.out.println("b.x = " + b.x);
System.out.println("b.y = " + b.y);
}
}
package class_and_object;
public class Static_Constant {
final static double PI = 3.1415926; //静态常量用final static来修饰
public static void main(String[] args) {
// TODO Auto-generated method stub
double radius = 3.0;
double area = Static_Constant.PI * radius * radius;
double volume = 4 / 3 * Static_Constant.PI * radius * radius * radius;
Circular yuan = new Circular(radius, area);
Spherical qiu = new Spherical(radius, volume);
}
}
class Circular {
double radius;
double area;
public Circular(double radius, double area) {
this.radius = radius;
this.area = area;
System.out.println("圆的半径为" + this.radius + " 圆的面积是" + this.area);
}
}
class Spherical {
double radius;
double volume;
public Spherical(double radius, double volume) {
this.radius = radius;
this.volume = volume;
System.out.println("球的半径为" + this.radius + " 球的体积是" + this.volume);
}
}
(三)静态方法
package class_and_object;
public class Static_Method {
// public void show() {
// System.out.println("hello");
// }
//
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// Static_Method a = new Static_Method();
// a.show();
// }
static public void show() {
System.out.println("hello"); //System其实也是一个类,通过类 直接调用静态方法
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Static_Method.show(); //静态方法是 斜体
}
}
package class_and_object;
public class Static_Code_Block {
static {
System.out.println("这里是静态代码块");
}
{
System.out.println("这里是非静态代码块");
}
public Static_Code_Block(){
System.out.println("这里是构造方法");
}
public void show() {
System.out.println("这里是成员方法");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Static_Code_Block a = new Static_Code_Block();
a.show();
}
}
静态代码块和非静态代码块是在构造方法之前运行的,也就是说,我们这个对象还没创建出来的时候 就执行了这两个代码块
package class_and_object;
public class Static_Code_Block {
static String name;
static { //静态的代码块中 要用静态成员变量
System.out.println("这里是静态代码块" + name);
}
{
System.out.println("这里是非静态代码块" + name);
}
public Static_Code_Block(String name){
this.name = name;
System.out.println("这里是构造方法" + name);
}
public void show() {
System.out.println("这里是成员方法" + name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Static_Code_Block a = new Static_Code_Block("abc");
a.show();
}
}
这也说明了,在构造方法运行之前,前面两个代码块已经首先执行了。
由此可见,在这里只有静态代码块执行了,也就是说这个静态代码块在声明类对象的时候就会执行
9、 类的主方法
静态的方法只能调用静态的变量,所以我们要在主方法中调用类的成员变量的话,只能调用静态的,否则我们要将这个类实例化
package class_and_object;
public class Class_MainMethod {
public void main2() {
System.out.println("我是成员方法");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("我是主方法");
System.out.println(args.length);
Class_MainMethod a = new Class_MainMethod();
a.main2();
}
}
package class_and_object;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(String tmp: args) {
System.out.println(tmp);
}
System.out.println("----end------");
}
}
除此之外,还有一种参数大家要知道:在java中,我们可以指定java虚拟机运行时 它的最大内存和最小内存,而这个参数我们写在了这里:
-是java虚拟机的参数,分别是java虚拟机运行时的最小内存和最大内存。有时候我们会遇到java虚拟机内存溢出,这是因为我们的内存不够了,我们就可以用这种方法 来将它运行时的内存扩大。
在下图中已经给他分配了最大内存和最小内存
下图中分别是运行时的参数 和 java虚拟机的参数
这样,java虚拟机的参数就配置好了
以上,便是java主方法的一个特点 以及它的使用方法
我们在控制台想要调用它的参数呢就直接敲命令;而在eclipse中想要调用它的参数,需要自己手动配置