类和对象
最简单的类
package study; public class test1{ public int brand; public String name; public void read(){ System.out.println(brand+"book"); } public void eat(int a){ System.out.println(name+" eat "+a+" meal"); } } package study; public class test { public static void main(String[] args) { test1 a=new test1(); a.brand=1; a.name="lihua"; a.read(); a.eat(3); } }
private可以使外部创建的对象无法直接访问到类里的各个成员,而是要通过事先定义好的各个方法进行访问
this用于当形参与成员名相同时进行区别,同时也可以作用于多个对象
制作一个简单的标准类
package study; public class test1{ private int number; private String name; public test1(){} public test1(int a,String b){this.number=a;this.name=b;} public void setNumber(int c){this.number=c;} public int getNumber(){return this.number;} public void setName(String d){this.name=d;} public String getName() {return name;} } package study; public class test { public static void main(String[] args) { test1 a=new test1(); a.setName("lihua"); a.setNumber(10); System.out.println(a.getName()); System.out.println(a.getNumber()); test1 b=new test1(20,"linan"); System.out.println(b.getName()); System.out.println(b.getNumber()); } }
字符串
基本数据类型作比较时,比较的是数值大小,当引用类型作比较时,比较的是地址
字符串直接比较使用的是地址值,可以使用equals函数比较字符串内容
String和Stringbuilder的区别在于String的内容不可变,Stringbuilder的内容可变
Stringbuilder可以通过append添加内容和reverse反转字符串,优点是不需要额外创建对象
string和stringbuilder相互转换,并完成反转
public class Main { public static void main(String[] args) { String s="helloworld"; StringBuilder str=new StringBuilder(s); str.append("people"); str.reverse(); str.toString(); System.out.println(str); } }
集合基础ArrayList
import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String[] args) { //尖括号内指定类型 ArrayList<String> arr=new ArrayList<String>(); //不限长度 arr.add("hello"); arr.add("world"); arr.add("java"); System.out.println(arr); //指定添加 arr.add(1,"china"); System.out.println(arr); //删除元素,返回值为布尔类型 System.out.println(arr.remove("china")); //删除指定元素,并返回元素值 System.out.println(arr.remove(1)); //获取指定位置的元素值 System.out.println(arr.get(1)); //返回集合的大小 System.out.println(arr.size()); //修改集合的元素值 System.out.println(arr.set(1,"anything")); System.out.println(arr); } }
继承
super关键字可以访问父类成员
this可以用于访问本类成员
子类创建对象会自动调用父类的无参构造方法
成员和方法的访问顺序是先访问子类的局部变量和方法,然后是子类的全局变量和方法,最后是父类的成员和方法
子类不能继承父类的私有成员,子类不能继承多个父类,但可以线性继承
子类要想继承父类就必须要比父类的访问权限高,public>默认>private
package study; public class test { public static void main(String[] args) { test1 a=new test1(); a.setName("lihua"); a.setNumber(10); System.out.println(a.getName()); System.out.println(a.getNumber()); test1 b=new test1(20,"linan"); System.out.println(b.getName()); System.out.println(b.getNumber()); test2 c=new test2(); c.setNumber(1); c.setName("hello"); c.showthis();; } } package study; public class test1{ private int number; private String name; public test1(){} public test1(int a,String b){this.number=a;this.name=b;} public void setNumber(int c){this.number=c;} public int getNumber(){return this.number;} public void setName(String d){this.name=d;} public String getName() {return name;} public void show(){ System.out.println("helloworld"); } } package study; public class test2 extends test1{ public test2(){ System.out.println("extenf the test1"); } public void showthis() { System.out.println(getNumber()+getName()); } }
继承猫和狗
package extendtest; public class animal { private int age; private String name; public animal() { System.out.println("this is a animal"); } public animal(int age, String name) { this.age = age; this.name = name; } public int getAge() { return age; } package extendtest; public class cat extends animal{ private int age; private String name; public void lookdoor(){ System.out.println("look the door"); } } package extendtest; public class dog extends animal{ public int age; public String name; public void catcha(){ System.out.println("catch the mouse"); } } package extendtest; import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput; public class demo { public static void main(String[] args) { cat c=new cat(); c.setName("hello"); System.out.println(c.getName()); c.setAge(10); System.out.println(c.getAge()); c.lookdoor(); dog d=new dog(); d.setName("world"); d.setAge(9); System.out.println(d.getName()); System.out.println(d.getAge()); d.catcha(); } }
包其实就是文件夹,可以通过import来导包,多级包可以用.来隔开
同一个包中的父类可以访问全部权限,同一个包中的子类和无关类不能访问private权限,不同包中的子类不能访问private和默认类,不同包中的无关类只能访问public类型
final修饰符修饰变量时,变量不可以被修改,修饰函数时,函数不能被重写,修饰类时,类不可以被继承
final修饰修饰局部变量时,如果变量是数据类型,其值不可以改变,如果是引用类型,其地址值不可以改变
static修饰类中成员,该成员能够被全部对象共享,可以通过对象或者类名直接访问
static修饰局部变量或方法时,静态方法只能访问静态变量
多态
将父类引用给子类,前提是子类继承了父类
多态的规则是:
变量编译看赋值表达式左面,执行也看赋值表达式左面
方法编译看赋值表达式左面,执行看赋值表达式右面
原因是方法可以被重写,但是变量不可以
多态中父类引用指向子类对象为向上转型,反之为向下转型
多态好处是可以扩展程序,让父类可以抽象为需要的子类
但是缺点是丧失了子类的特殊性
抽象类
如果一个方法中没有方法体,那么这个方法为抽象方法,一个包含抽象方法的类称为抽象类
抽象类的子类要么也是抽象类,要么就得重写抽象类中的抽象方法
抽象类要通过多态来实例化
package abtracttest; public abstract class animal { private int age; private String city; public abstract void eat(); public animal() { } public animal(int age, String city) { this.age = age; this.city = city; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } package abtracttest; public class cat extends animal{ public int age=20; @Override public void eat() { System.out.println("cat eat fish"); } public void playgame(){ System.out.println("catch the mouse"); } } package abtracttest; public class dog extends animal{ @Override public void eat() { System.out.println("dog eat bone"); } } package abtracttest; public class animaldemo { public static void main(String[] args) { animal a=new cat(); a.setAge(10); a.setCity("shanghai"); System.out.println(a.getAge()); System.out.println(a.getCity()); a.eat(); animal b=new dog(); b.setAge(11); b.setCity("sichuan"); b.eat(); System.out.println(b.getAge()); System.out.println(b.getCity()); } }
package abtracttest;
public class animal {
public int age=60;
public void eat(){
System.out.println("animal eat food");
}
}
package abtracttest;
public class animalcatch {
public void animalhunt(animal a){a.eat();}
}
package abtracttest;
public class animaldemo {
public static void main(String[] args) {
animal a=new cat();
System.out.println(a.age);
cat c=(cat)a;
c.playgame();
animalcatch ca=new animalcatch();
ca.animalhunt(c);
}
}
package abtracttest;标签:java,入门,void,System,接口,String,println,public,out From: https://www.cnblogs.com/alexlance/p/18399259
public class cat extends animal{
public int age=20;
@Override
public void eat() {
System.out.println("cat eat fish");
}
public void playgame(){
System.out.println("catch the mouse");
}
}