Android 之路12---Java基础7

导读

1.代码的封装
2.使用包进行类管理
3.static关键字
4.代码块

代码的封装

package com.hala.cat;

public class Cat {
	//属性
	private String  name;
	private int     age;
	private double  weight;
	//private只允许属性在该类内被操作和访问,而不许在类外被访问
	//方法
	
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return this.name;
	}
	//上边两个方法就是创建公有get/set方法
	
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if(age<0)
			System.out.println("age值必须不小于0");
		else this.age = age;
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		if(weight<0)
			System.out.println("weight值必须不小于0");
	//这里可以限制给属性传值的范围,当然也可以通过异常处理来限制
		else this.weight = weight;
	}
	
	
	public void eat(){
		this.run();
		//this不仅可以穿属性时调用,还可以调用函数
		System.out.println("小猫吃鱼");
	}

	public void run(){
		System.out.println("小猫快跑");
	}

}

package com.hala.cat;

public class CatTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Cat one=new Cat();
		one.setName("小黄");
		one.setAge(-1);
		one.setAge(6);
		one.setWeight(7.7);
		System.out.println("name:"+one.getName());
		System.out.println("name:"+one.getAge());
		System.out.println("name:"+one.getWeight());
	}

}

输出结果
age值必须不小于0
name:小黄
name:6
name:7.7

注意,用构造函数也可以给私有属性赋值,但这样不会限定范围,解决方法是,在构造函数里调用set函数

快捷键,在代码空白区域右击按下图会快捷设置属性的get和set方法

使用包进行类管理

同一个包内不允许有同名的类,但不同的包下可以有同名类

导入包的三种方式

方式一:

package com.hala.test;//定义包,必须写在第一行,且只能有一条语句

import com.hala.cat.*;//导入了这个包的所有类

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Cat one =new Cat();
	

	}

}

输出结果
我是宠物猫

方式二:

package com.hala.test;//定义包,必须写在第一行,且只能有一条语句

import com.hala.cat.Cat;//用哪个类导入哪个类

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Cat one =new Cat();
	

	}

}

输出结果

我是宠物猫

方式三:

package com.hala.test;//定义包,必须写在第一行,且只能有一条语句



public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		com.hala.cat.Cat one =new com.hala.cat.Cat();
	

	}

}

输出结果

我是宠物猫

辨析

package com.hala.test;//定义包,必须写在第一行,且只能有一条语句

import com.hala.cat.*;
import com.hala.machines.Cat;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cat two=new Cat();//此时会优先选择那个专门导入Cat类的包调用,即com.hala.machines.Cat;
		com.hala.cat.Cat one =new com.hala.cat.Cat();
	//若想用cat包里的Cat类,就要这样写

	}

}

输出结果

我是机器猫
我是宠物猫

在这里插入图片描述

在这里插入图片描述

static关键字

public class Cat {
	//属性
	private String  name;
	private int     age;
	private double  weight;
	
	public static int price;
package com.hala.test;//定义包,必须写在第一行,且只能有一条语句

import com.hala.cat.*;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cat two=new Cat();
		
		two.price=200;
		Cat.price=300;
		//static属性可以通过对象修改,也可以通过类名修改
		//而且一旦修改,就是把这个类所有对象的这个属性修改了,也就是类的成员共用的
		System.out.println("price:"+two.price);
	}

}

static添加的位置

static 可以加在类的属性前,方法前,静态代码块。如下

public static int price;
public static void run(){
}

static{}

⚠️在非静态的方法中可以调用静态的属性或方法,但反过来不能在静态的方法中调用非静态的属性和方法。可以通过实例化一个对象,再通过对象调用非静态成员

但不能加在类前边,和局部变量前。

代码块

普通代码块:写在方法里
构造代码块:与方法并列写
静态代码块:在构造代码块前加static

调用顺序:静态->构造->普通

package com.hala.machines;

public class Cat {
	
	
	//属性
		private String  name;
		private int     age;
		private double  weight;
		
		public static int price;
		
		//方法
		
		{
			System.out.println("我是构造代码块2");
		}
		
		{
			System.out.println("我是构造代码块1");
		}
		//构造代码块在产生对象时,构造方法之前被调用
		//相同类型的代码块按照顺序被调用,即上边先调用,下边后调用
		
		static{
			System.out.println("我是静态代码块");
		}
		//静态代码块在产生类时被调用,所以不管实例化多少对象它只调用一次
		//静态代码块里只能调用静态成员
	public Cat(){
		System.out.println("我是机器猫");
	}
	
	public void run(){
		{
			System.out.println("我是普通代码块2");
		}
		System.out.println("小猫快跑");
		{
			System.out.println("我是普通代码块1");
		}
	}

}

package com.hala.test;//定义包,必须写在第一行,且只能有一条语句

import com.hala.machines.*;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cat one=new Cat();
		Cat two=new Cat();
		one.run();
		two.run();
		
	}

}

输出结果
我是静态代码块
我是构造代码块2
我是构造代码块1
我是机器猫
我是构造代码块2
我是构造代码块1
我是机器猫
我是普通代码块2
小猫快跑
我是普通代码块1
我是普通代码块2
小猫快跑
我是普通代码块1

代码块与局部变量

//这样写是错误的,temp重复命名

public void run(){
		int temp=1;
		int temp=2;
//这样写是错误的,temp重复命名
		{
			
			System.out.println("我是普通代码块2");
		}
		System.out.println("小猫快跑");
		{
			System.out.println("我是普通代码块1");
		}
	}

//这样写是正确的,代码块自成一体

	public void run(){
		
		{
			int temp=1;
			System.out.println("我是普通代码块2,temp:"+temp);
		}
		System.out.println("小猫快跑");
		{
			int temp=2;
			System.out.println("我是普通代码块1.temp:"+temp);
		}
	}

package com.hala.test;//定义包,必须写在第一行,且只能有一条语句

import com.hala.machines.*;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cat one=new Cat();
		
		one.run();
		
		
	}

}

输出结果
我是普通代码块2,temp:1
小猫快跑
我是普通代码块1.temp:2

//这样写又是错误的,第一个temp生命周期为整个方法

	public void run(){
		int temp=3;
		{
			int temp=1;
			System.out.println("我是普通代码块2,temp:"+temp);
		}
		System.out.println("小猫快跑");
		{
			int temp=2;
			System.out.println("我是普通代码块1.temp:"+temp);
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值