JAVA基础

JAVA基础

方法重载

方法的重载是面向对象程序多态的一种实现策略,表现是在同一个类中的多个同名方法的不同实现。

public class PrintDemo {

	
	public void write(float f){
		System.out.println(f);
	}
	
	public void write(String s){
		System.out.println(s);
	}
	public void write(int i){
		System.out.println(i);
	}
	
	public static void main(String[] args) {
		PrintDemo pd = new PrintDemo();
		pd.write(1.23F); 
		pd.write("hello");
		pd.write(1);
	}
}

注意事项

  1. 方法的重载一般发生在同一个类或者存在继承关系的多个类中
  2. 重载必须要保证被重载方法参数类型,个数,顺序任意有一项不一致
  3. 方法的重载跟返回值无关

方法的重写和重载都是多态一种实现策略

this&super

this

this关键字在一个类中代表当前类的实例对象,外界创建对象后,通过对象的引用调用方法时,在方法内部可以使用this来获取对象自身,this关键字可以在类中,调用当前类属性,方法和构造器。

例如:

public class User {

	private String name;
	
	public User(){
		System.out.println("默认构造器");
	}
	
	public User(String name){
		this();
		this.name = name;
	}
	
	public void setName(String name){
		this.name = name;
		System.out.println(this); 
	}
	
	public String getName(){
		return this.name;
	}
	
	public void m(){
		System.out.println("m方法");
	}
	
	public void n(){
		this.m();
	}
	
}

public class Test {

	public static void main(String[] args) {
		User u = new User("soft");
		System.out.println(u);
	}

}

super

super一般用于存在继承关系的子类中,子类可以通过super关键字,调用父类中属性,方法,构造器,super并不是一个父类对象。

public class Animal {

	String name;
	
	public Animal(){
		System.out.println("animal构造器被执行");
	}
	
	public void sleep(){
		System.out.println("睡觉");
	}
}

public class MeatAnimal extends Animal{

	public void sleep(){
		System.out.println(super.name+"趴着睡");
	}
}
public class Dog extends MeatAnimal{

	public Dog(){
		super();
	}
	
	public void eat(){
		System.out.println(super.name+"吃狗粮");
		super.sleep();
	}
}

多态

多态是面向对象三大特征中最重要的一种特征,多态可以体现方法层面,方法层面的多态由重载和重写实现;另外多态最重要的体现在变量层面,可以使用一个父类的引用变量指向任何的子类对象,从而实现引用动态绑定机制。

多态实例

public class Animal{
    String name;
    public Animal(String name){
        this.name = name;
    }
    
    public void eat(){
        System.out.println(name+"吃东西");
    }
}
public class Dog extends Animal{
    public Dog(String name){
        super(name);
    }
}
public class Test{
    public static void main(String[] args){
        //父类引用指向子类对象(向上转型)
        Animal a = new Dog();
        //下转型
        Dog d = (Dog)a;
    }
}

使用多态可以提高程序的可扩展性,多态也称之为动态绑定

可以使用父类引用指向子类对象,但是如果使用子类引用指向父类对象时,操作不当将会导致类型转换异常

java.lang.ClassCastException

基本数据封装类型

java中基本数据类型由于只能做一些算数或者比较运算,因此在实际使用中可能有些复杂操作需要处理,比如将String类型转换为int型,或者获取int类型的各种进制的表现形式。所以java中对8个基本类型数据提供了包装类型
在这里插入图片描述

实例参考

public class Test {

	public static void main(String[] args) {
		
		int i = 10;
		double d = 3.14;

		String s = i+"";
		
		//基本数据类型的封装类型
		Integer it = new Integer(i);
	
		System.out.println(Integer.MIN_VALUE); 
		System.out.println(Integer.MAX_VALUE); 
		//1  0000 0001
		//2  0000 0010
		//3  0000 0011
		//4  0000 0100
		//获取指定十进制整数转换为二级制后里面1的位数
		System.out.println(Integer.bitCount(847574));
		//将十进制整数转换为二进制字符串
		System.out.println(Integer.toBinaryString(847574));
		//将十进制整数转换为16禁止字符串
		System.out.println(Integer.toHexString(847574));
		
		//将字符串转换为整数
		System.out.println(Integer.parseInt(s) + 10);
		
		System.out.println(Integer.parseInt("-100") + 10);
		
		System.out.println(Integer.sum(10, 20));
		System.out.println(Integer.max(50, 100));
		
		//自动装箱
		Integer in = 100;
		
		System.out.println(in.toString());
		
		//自动拆箱
		int j = new Integer(100);
		
		Character c = 'a';
		System.out.println(Character.isLowerCase(c));
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值