最近一段时间痴迷于java,于是就学了两三周,总结了一些基础知识!
希望对您们有一丢丢帮助!嘻嘻!
Java中父类和接口的关系
interface A{
int x=1;
}
class B{
int x=0;
}
public class Test extends B implements A{
public void print(){
System.out.println(x);
}
public static void main(String[] args) {
new Test().print();
}
}
//你会发现编辑器报错
因为父类和接口是平行关系,所以不知道要调用那一个,如果实在要调用,可以这样:
interface A{
int x=1; //public static final 隐藏了
}
class B{
int x=0;
}
public class Test extends B implements A{
public void print(){
System.out.println(super.x); //父类的x
System.out.println(A.x); //接口的x(全局变量)
}
public static void main(String[] args) {
new Test().print();
}
}
equals方法
在Object类中,equals方法为:
public boolean equals(Object obj){
return (this==obj); //相当于==的作用(即地址是相同的)
}
但是在像String,Date,File,以及包装类等都重写了Object类中的equals()方法,变为比较两个对象的“实体类容”是否相等!
下面举了一个例子:
class Customer{
int age;
String name;
public Customer(int age,String name){
this.age=age;
this.name=name;
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(obj instanceof Customer){
Customer c=(Customer)obj;
return this.age==c.age&&this.name.equals(c.name);
}
return false;
}
}
public class Test2 {
public static void main(String[] args) {
Customer c1=new Customer(2,"gl");
Customer c2=new Customer(2,"gl");
System.out.println(c1.equals(c2));
}
}
==如果是基本数据类型,则比较内容是否相同,如果是引用数据类型,则比较的是地址。
==两边的数据类型应该是一致的
equals方法只适用于引用数据类型
String类型的内存机制
public static void main(String[] args) {
String x=new String("sd");
String y=new String("sd");
String z="sd";
String m="sd";
System.out.println(z==m);
System.out.println(x==y);
}
//结果为true false
不同的定义方法得到不同的结果
如果是第二种方法,在常量池中定义了很多的常量,第二种方法就是将常量的地址赋给变量名,所以结果为true。
toString方法
super调用构造器:
我们可以在子类构造器中显式调用父类的构造器
1.super(形参列表)必须放在子类构造器的首行!
2.在类的一个构造器中super()和this()只能二选一,不能同时出现!
4.在子类构造器首行,如果没有执行super(形参列表)或者this(形参列表),会默认执行super()。
5.在子类的构造器中,至少有一个构造器使用了super(形参列表)方法。
class Person{
int age;
String name;
public Person(){}
public Person(int age,String name){
this.age=age;
this.name=name;
}
}
class Student extends Person{
int id;
public Student(int id){
super() //默认执行
this.id=id;
}
public Student(int age,String name,int id){
super(age,name); //执行有参构造器就不会执行无参构造器
this.id=id;
}
public Student(int id,int age){
super() //默认执行
this(id);
this.age=age;
}
public void show(){
System.out.println("age:"+this.age+",name:"+this.name);
}
}
public class Test4 {
public static void main(String[] args) {
Person per1=new Person(2,"ds");
Student stu1=new Student(1,"re",45);
stu1.show();
}
}
包装类:
public class Test6{
public static void main(String[] args) {
Integer x=new Integer(29);
int number = x.intValue(); //将包装类转化为基本数据类型
System.out.println(x.toString()+2); //返回为String类型
Integer s=new Integer("34");
System.out.println(s);
Boolean s1=new Boolean(true);
Boolean s2=new Boolean("true");
Boolean s4=new Boolean("TRue"); //不会区分大小写
Boolean s3=new Boolean("true3"); //只要不是true大小写类型的就会返回false
System.out.println(s3); //默认调用toString方法
}
}
自动拆箱和装箱
public class Test{
int x = 5;
Integer num = 5; //自动装箱
boolean str = true;
Boolean str1=str; //自动装箱
int nunber = num; //自动拆箱
boolean str2 = str1; //自动拆箱
}
public class Test6{
public static void main(String[] args) {
int x = 5;
boolean sum=true;
Integer num = new Integer(5);
String str = String.valueOf(x); //将基本数据类型改为字符串
String str1=String.valueOf(num); //包含一个自动拆箱 同时将包装类转化为字符串
Integer y = Integer.parseInt("45");
Boolean m=Boolean.parseBoolean("True");
System.out.println(m);
System.out.println(y);
Integer sum1 = 1;
Integer sum2 = 1;
System.out.println(sum1==sum2); //返回值为true
Integer sum3=128;
Integer sum4=128;
System.out.println(sum3==sum4); //返回值为false
//在Integer的常量池中范围为-128——127,如果超出了这个范围就会重新new,这样做的目的也就是提高效率
}
}
数组可以看成Object类的子类!
例如:
public class Test {
public void test(Object obj){
System.out.println(obj);
}
public static void main(String[] args) {
int arr[]=new int[10];
System.out.println(arr.toString()); //数组可以看成Obiect类的子类
Test s=new Test();
s.test(arr);
}
}
//输出结果为:
//[I@154617c
//[I@154617c
static属性
static修饰变量时,这个变量不属于某一个对象,而是属于这个类,由所以对象所共有!
下面可以举一个例子:
public class Test1 {
static String nation = "中国";
String name;
int age;
public Test1(String name,int age){
this.age=age;
this.name=name;
}
public static void main(String[] args) {
Test1 person1 = new Test1("姚明",40);
Test1 person2 = new Test1("马龙",30);
System.out.println(person1.nation);
person2.nation="美国"; //修改对象二所对应的类属性
System.out.println(person1.nation); //对象一对应的类属性也发生了改变
}
}
//输出结果为:
//中国
//美国
ststic修饰方法:和修饰属性类似!
只不过在静态方法中,不能调用非静态方法和非静态属性!
但在非静态方法中,既可以调用静态方法也可以调用非静态方法!
public class Test1 {
static String nation = "中国";
String name;
int age;
public Test1(String name,int age){
this.age=age;
this.name=name;
}
static {
System.out.println("静态代码块只会执行一次,因为类只加载一次!");
System.out.println("静态代码块会随着类的加载而执行!");
nation="英国";
}
static{
System.out.println("静态代码块可以有多个,并且按顺序执行");
}
{
System.out.println("代码块会随着对象的创建而执行!");
System.out.println("代码块可以执行多次!");
this.age=20; //在代码块中对对象属性赋值
}
{
System.out.println("代码块也可以有多个,并且按顺序执行!");
}
public static void main(String[] args) {
Test1.nation="美国";
System.out.println(Test1.nation); //结果为美国!
Test1 person1 = new Test1("姚明",40); //在构造器中对属性赋值
Test1 person2 = new Test1("马龙",30);
System.out.println(person1.age);
}
}
//结果age的值为20,所以非静态代码块比构造器优先!
main()方法
它是一个公共的静态的没有返回值的方法,作为程序的入口!
它的调用相当于类.main() 这个类是你自己写的包含main()方法的类!
所以在调用main()方法之前会先加载类!
public class Test2 {
static{
System.out.println("Test2的静态代码块");
}
{
System.out.println("Test2的代码块");
}
}
class T extends Test2 {
static {
System.out.println("T的静态代码块");
}
{
System.out.println("T的代码块");
}
}
class E extends T {
static {
System.out.println("E的静态代码块");
}
{
System.out.println("E的代码块");
}
public static void main(String[] args) {
System.out.println("3");
new E();
}
}
//输出结果为:
//Test2的静态代码块
//T的静态代码块
//E的静态代码块
//3
//Test2的代码块
//T的代码块
//E的代码块
final修饰方法和类:
修饰方法时,表示不能被重写!
修饰类时,表示不能被继承!
final修饰变量时
“变量”会变成常量,且不能被修改!
public class Test3 {
int age;
final int num = 10; //1.直接显示赋值
final int sum; //如果只定义不赋值会报错!可以在代码块或者构造器中赋值!
{
//sum=2; //代码块和构造器不能同时对final变量进行赋值!
}
public Test3(int sum){
this.sum=num; //在构造器赋值!
}
public Test3(int sum,int age){
this.sum=num;
this.age=age; //如果有多个构造器则必须在每个构造器中都对final未赋值的变量赋值!
}
}
如果既有final修饰还有static修饰,一般表示全局常量!
内部类
public class Test4 {
public static void main(String[] args) {
Person.dog dog1=new Person.dog(); //实例化静态内部类
dog1.show();
Person per=new Person();
Person.bird bird1=per.new bird(); //实例化非静态内部类!
bird1.fly();
}
}
class Person{
static class dog{
public void show(){
System.out.println("你是一条狗!");
}
}
class bird{
public void fly(){
System.out.println("我会飞!");
}
}
}
现在的你可能会有些辛苦,会熬夜敲代码,休息时间可能有些少,但是未来你一定会感谢现在的你!能够有足够的钱随便买东西!让我们一起加油吧!博友们!