继承的语法规则:如果B类继承A类
public class B extends A{}
B类称之为:子类
A类称之为:父类、基类、超类
父类中只有被public、protected等修饰的属性或者方法才能够被子类继承;如果想父类中的某一属性或者方法不被子类继承,使用private访问修饰符修饰。
如果某一类充当父类,优先将此类中的属性使用protected修饰;其他情况下,属性优先考虑使用private修饰。
子类如何调用父类的属性和方法:前提是父类中的属性和方法能够被子类继承
1、子类如何访问父类中的属性:super.父类的属性名;
2、子类如何访问父类中的方法:super.父类的方法名;
子类如何调用父类的构造方法:
当创建子类的对象时,子类的构造方法一定会去调用父类的构造方法。
1.如何在子类中显示调用父类的无参构造方法:super();
public class Dog extends Pet{
private String strain;//品种
public Dog(){
super();//此行代码必须放置在第一行
System.out.printLn("Dog.....无参构造方法");
}
}
2、如何在子类中调用父类中的有参构造方法:super(实参1,实参2…)
public Dog(){
super("宠物",10);
System.out.printLn("Dog....无参构造方法");
}
所有类的祖宗类:所有类都默认继承Object类,但是如果某个类显示继承其他类,就不会继承Object。Object类是所有类的祖宗类。
终结类:被final修饰符修饰的类不能够被其他类继承。
包装类:由于在java中万物皆对象,但8种基本数据类型不是类类型,违背了面向对象的思想。此时为8种数据基本数据类型提供相对应的包装类
int a1=20;
Integer a2=30;
Integer a3=new Integer(30);
char ch1='中';
Character ch2=new Character(ch1);
Integer类中常用的方法:
public static void main(String[] args) {
Integer i = 20;
int a = 30;
// 1将a变成一个字符串;
String str1 = a + "";
// 2将数值类型的字符串转成int类型
String str2 = "123";
int b = Integer.parseInt(str2);
// 3.将二进制转成10进制
String str3 = "11111111";
int c = Integer.parseInt(str3, 2);
System.out.println(c);
//
String str4 = "3.14F";
float f1 = Float.parseFloat(str4);
System.out.println(f1);
String str5 = "3.14";
double d = Double.parseDouble(str5);
}