一、继承
1、JAVA类的继承只允许单继承,接口才存在多继承
2、extends 实现继承
3、Ctrl+T查看继承顺序,一个类没有写继承时它的父类为Object
4、 instanceof判断,当对象是右面类或子类所创建对象时,返回true;否则,返回false(左对象,右类)
public class JiCheng {
public static void main(String[] args) {
Person Pr = new Person();
Pr.play();
Pr.rest();
System.out.println(Pr instanceof Student);
System.out.println(Pr instanceof JiCheng); //类里面是否存在这个对象,这种情况是不可能出现的所以会报错
}
}
class Person extends Student{ //extends让Person继承了Student
void sleep() {
System.out.println("睡觉八");
}
}
class Student { //注意函数里面不能嵌套函数,这里Student可以用Ctrl+T查看父类为Object
void play(){
System.out.println("出去玩");
}
void rest() {
System.out.println("休息");
}
}
二、重写
1、方法名、形参列表相同
2、子类要小于父类才能重写
public class Override {
public static void main(String[] args) {
Flower Fl = new Flower();
Fl.color();
}
}
class Flower extends Forest{
void color() { //直接将父类的方法覆盖了 "=="形参列表相同
System.out.println("red");
}
void sex() {
System.out.println("women");
}
}
class Forest{
void color() {
System.out.println("all color");
}
}
补充!!== ; >= ; <=
三、 Object的用法
1、所有子类都可以继承Object的方法,Object是类里面最大。
2、toString
代码Ctrl点击toString可以查看源码
在打印输出或者用字符串连接对象时,会自动调用该对象的toString()方法
public class Object1 {
int age;
int id;
public static void main(String[] args) {
Object1 Ob = new Object1();
Ob.age = 11;
Ob.id = 1;
System.out.println("age:"+Ob); //自动调用toString方法
}
public String toString(){ //重写
return age +"\n"+ "id:" +id;
}
}
//Object的重写
package JiCheng;
public class Object1 {
public static void main(String[] args) {
Object1 Ob = new Object1();
System.out.println(Ob.toString());
}
public String toString(){ //重写这个方法需要有返回值
return "hell";
}
}
3、_equals方法
没有改写的equals方法相当于 ==
public class Object1 {
int age;
int id;
public Object1(int age, int id) { //构造方法
super();
this.age = age;
this.id = id;
}
public boolean equals(Object obj) { //重写,让id相同就判断为true
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Object1 other = (Object1) obj; //强制类型转化
return id == other.id; //同true异false
}
public static void main(String[] args) {
Object1 Ob = new Object1(11,4);
Object1 Oa = new Object1(1,4);
System.out.println(Ob.equals(Oa));
}
}
tip:自动生成方法
四、super父类的引用
1、从他的父类向上访问是否存在属性h直到Object类,再从Object类返回下来
2、如果没找到,则出现编译错误
3、每一个构造函数第一行无论写与不写super(),系统默认存在
public class Pro1 {
public static void main(String[] args) {
ChildrenClass Ch = new ChildrenClass(); //new对象时直接调用构造方法
}
}
class FatherClass{
public FatherClass() { //构造方法需要public
System.out.println("11");
}
}
class ChildrenClass extends FatherClass{
public ChildrenClass() {
//super() 无论加不加super,构造函数第一行都会默认super
System.out.println("22");
}
}
五、封装(具体见JAVA封装使用一文)
1. private表示私有,只有自己类能访问
- private定义的参数在其他类无法查看
public class Test {
public static void main(String[] args) {
Play Te = new Play();
Te.play();
System.out.println(Te.a);
}
}
class Play{
int a; //就可以在另外的类中查看Play的参数
void play(){
System.out.println(a);
}
}
- 子类继承了父类但也无法调用父类私有属性和方法
class Play{
int a; //位于同一个包都能用
void play(){
System.out.println(a);
}
}
class Run extends Play{
void Run() {
System.out.println(a);
}
}
2. default表示没有修饰符修饰,只有同一个包的类能访问
3. protected表示可以被同一个包的类以及其他包中的子类访问
4. public表示可以被该项目的所有包中的所有类访问
六、多态
1. 多态是方法的多态,不是属性的多态(多态与属性无关)。
2. 多态的存在要有3个必要条件:继承,方法重写,父类引用指向子类对象。
3. 父类引用指向子类对象后,用该父类引用调用子类重写的方法
class Animal {
public void shout() {
System.out.println("叫了一声!");
}
}
class Dog extends Animal {
public void shout() {
System.out.println("旺旺旺!");
}
public void seeDoor() {
System.out.println("看门中....");
}
}
class Cat extends Animal {
public void shout() {
System.out.println("喵喵喵喵!");
}
}
public class Test_2 {
public static void main(String[] args) {
Animal a1 = new Cat(); // 向上可以自动转型
//传的具体是哪一个类就调用哪一个类的方法。大大提高了程序的可扩展性。
animalCry(a1);
Animal a2 = new Dog();
animalCry(a2);//a2为编译类型,Dog对象才是运行时类型。
//编写程序时,如果想调用运行时类型的方法,只能进行强制类型转换。
// 否则通不过编译器的检查。
Dog dog = (Dog)a2;//向下需要强制类型转换
dog.seeDoor();
}
// 有了多态,只需要让增加的这个类继承Animal类就可以了。
static void animalCry(Animal a) {
a.shout();
}
/* 如果没有多态,我们这里需要写很多重载的方法。
* 每增加一种动物,就需要重载一种动物的喊叫方法。非常麻烦。
static void animalCry(Dog d) {
d.shout();
}
static void animalCry(Cat c) {
c.shout();
}*/
}
七、 final关键字
1、修饰变量: 被他修饰的变量不可改变。一旦赋了初值,就不能被重新赋值
int a=6;显示这里的a是不可见的
2、该方法不可被子类重写。但是可以被重载
这里不能被重写
下面final可以重载
package Test1;
public class Test_1{
int a;
int b;
Test_1(){
this(5);
System.out.println("haha");
}
Test_1(int a){
System.out.println("fafa"+a);
}
public static void main(String[] args) {
final Test_1 Th = new Test_1(); //重载的构造方法,通过new直接调用
}
}
3、修饰的类不能被继承。比如:Math、String等
因为父类前面加上了final,不允许子类继承
八、数组
1、数组的声明
int[] a;
String[] name = new String[10];
2、数组的初始化
// 静态初始化
int[] b = {22,1,2}; //花括号赋值
// 默认初始化
int[] c = new int[4];
// 动态初始化,数组一经分配空间,其中的每个元素也被按照实例变量同样的方式被隐式初始化
String[] d = new String[4];
d[0] = "sdf";
d[1] = "df";
3、数组的循环
- for循环可以写入和遍历
int[] b = {22,1,2}; //花括号赋值
for(int i=0;i<b.length;i++) {
System.out.println(b[i]);
}
- for-each只能遍历,且与下标无关
int[] b = {22,1,2}; //花括号赋值
for(int temp:b) {
System.out.println(temp);
}
4、 数组的拷贝
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);
System.arraycopy(原数组,复制过去的下标,copy的数组,放入的下标位置,复制过去的个数)
public class NumCopy {
public static void main(String[] args) {
int i;
String[] a = {"11","22","33","44","55"}; //每个都要双引号
String[] b = new String[10];
System.arraycopy(a,1,b,5,3);
for(i=0;i<b.length;i++) {
System.out.println("i:"+i+','+b[i]);
}
}
}
通过拷贝实现数组删除
- 用a数组覆盖a数组
- 3 从a数组下标为3的元素开始复制
- 3-1 将下标为3的元素放到a数组里面下标为2的位置
- a.length-3 为从a复制多少个元素到a, 如这里将"44"、"55"放到下标为2、3的位置
public class NumCopy {
public static void main(String[] args) {
int i;
String[] a = {"11","22","33","44","55"}; //每个都要双引号
String[] b = new String[10];
System.arraycopy(a,3,a,3-1,a.length-3);
for(i=0;i<a.length;i++) {
System.out.println("i:"+i+','+a[i]);
}
}
}
5、通过封装来实现删除函数
public class arry {
public static void main(String[] args) {
String[] a = {"11","淘宝","天猫","拼多多","88","阿里"};
arry ar = new arry();
ar.removeArry(a, 4);
}
public void removeArry(String[] j,int index) {
if(index >= j.length || index<0) {
System.out.println("错误");
return;
}
System.arraycopy(j, index+1, j, index, j.length-index-1);
j[j.length-1]=null; //null运行的时候就不会打印出来
for(int i=0;i<j.length-1;i++) {
System.out.println(j[i]);
}
}
}
6、封装实现数组增加元素
通过放在另一个大容量的数组来添加元素,a已经定义后数组的长度已经固定了
public class arry {
public static void main(String[] args) {
String[] a = {"11","淘宝","天猫","拼多多","88","阿里"};
String[] b = new String[10];
arry ar = new arry();
ar.addArry(a,b,1, "dsf");
}
public void addArry(String[] i,String[] p,int index,String j) {
System.arraycopy(i,0,p,0,i.length);
System.arraycopy(p, index, p, index+1,i.length-index);
p[index] = j;
for(int k=0;k<p.length;k++) {
System.out.println(p[k]);
}
}
}
7、封装数组扩容
public class arry {
public static void main(String[] args) {
String[] a = {"11","淘宝","天猫","拼多多","88","阿里"};
extend(a);
System.out.println(extend(a).length); //相当于s2的长度,将s2返回出来
for(int i=0;i<extend(a).length;i++) {
System.out.println(extend(a)[i]);
}
}
public static String[] extend(String[] s) {
String[] s2 = new String[10];
System.arraycopy(s, 0, s2, 0, s.length);
return s2;
}
}
8、Arrays工具类使用
import java.util.Arrays;
public class __Arrays {
public static void main(String[] args) {
String[] a = {"11","44","34","22","88","66"};
System.out.println(a);
System.out.println(Arrays.toString(a)); //以数组的形式出现
System.out.println(Arrays.binarySearch(a,"44")); //查找位置,无则返回-1
Arrays.sort(a); //正向排序
System.out.println(Arrays.toString(a));
}
}
9、多维数组
public class ManyArry {
public static void main(String[] args) {
int[][] a = new int[3][]; //动态数组
a[0] = new int[]{12,22}; //二维数组的格式
a[1] = new int[] {11,43};
System.out.println(a[1][1]);
int[][] a1 = { //静态设置数组
{12,22},
{23,66},
};
System.out.println(a1.length); //第一维数组的长度
System.out.println(a1[0].length); //第二维数组的长度
}
}
10、多维数组实现表单
import java.util.Arrays;
public class ManyArry {
public static void main(String[] args) {
Object[] temp1 = {1,"哈哈",13,100}; //定义一维数组
Object[] temp2 = {2,"喜喜",14,88};
Object[] temp3 = {3,"可可",11,90};
Object[][] temp = new Object[3][]; //定义二维数组
temp[0] = temp1; //将一维数组放到二维数组里面
temp[1] = temp2;
temp[2] = temp3;
for(int i=0;i<3;i++) {
System.out.println(Arrays.toString(temp[i])); //通过导入包将数组打印出来
}
}
}
报错信息:
1、返回值要一致同输入的值一致
2、void 返回false停止,只能用return;
3、为什么一些不需要初始化对象就可以调用方法?
因为那些方法是静态方法
九、抽象类
abstract class Second {
abstract void func();
//不加花括号
}
class First extends Second{ //子类继承抽象类
void func() {
System.out.println("gg"); //子类实现抽象方法
}
}
public abstract class Abstract{
public static void main(String[] args) {
// Abstract Ab = new Abstract(); 抽象类不能new
First Ab = new First();
Ab.func();
}
}
1. 有抽象方法的类只能定义成抽象类
2. 抽象类不能实例化,即不能用new来实例化抽象类。
3. 抽象类可以包含属性、方法、构造方法。但是构造方法不能用来new实例,只能用来被子类调用。
4. 抽象类只能用来被继承。
5. 抽象方法必须被子类实现。
十、 面向接口编程
public interface JieKou {
public static void main(String[] args) {
Angel An = new Angel();
An.eat();
}
}
interface Volant{
int Ff =10;
void fly();
}
interface Honest{ //默认为abstract,还不能new对象
void helpOther();
}
interface Change extends Volant,Honest{ //接口实现
void eat();
}
class Angel implements Volant,Honest{
public void helpOther() {
System.out.println("help");
}
public void fly() {
System.out.println("飞");
}
public void eat() {
System.out.println("noodle");
System.out.println(Ff); //继承Volant和Honest
}
}
1、子接口可以实现多继承
2、访问修饰符:只能是public或默认。
3、常量:接口中的属性只能是常量,总是:public static final 修饰。不写也是
4、子类通过implements来实现接口中的规范
5、接口不能创建实例,但是可用于声明引用变量类型
6、一个类实现了接口,必须实现接口中所有的方法,并且这些方法只能是public的
十一、内部类
1、非静态内部类
public class Test {
int a = 1;
public static void main(String[] args) {
Test1.First Tf = new Test1().new First(); //实例了Test1和First对象
Tf.func();
}
}
class Test1{
private int age = 20;
class First{ //内部类
int age = 30;
public void func() {
int age = 10;
System.out.println("外部类年龄为:",Test1.this.age);
System.out.println("内部类年龄为:",this.age);
System.out.println("局部年龄为:",age);
}
}
}
- 非静态内部类必须寄存在一个外部类对象里
- 非静态内部类可以直接访问外部类的成员,但是外部类不能直接访问非静态内部类成员
- 非静态内部类不能有静态方法、静态属性和静态初始化块
- . 外部类的静态方法、静态代码块不能访问非静态内部类,包括不能使用非静态内部类定义变量、创建实例
2、静态内部类
十二、 String常用方法
1、字符串的拼接
public class __String {
public static void main(String[] args) {
String a = "123";
String d = "11";
int b = 11;
String c = b+a; //有字符串直接拼接
String e = a+d; //字符串拼接
System.out.println(c);
System.out.println(e);
}
}
2、判断字符串是否一致,一般建议用equals能判断值是否相同
public class __String {
public static void main(String[] args) {
String a = "123";
String d = "123";
String c = new String(d);
System.out.println(c); //虽然打印出来结果相同
System.out.println(c==a); //创建的地址空间不同,所有false
}
}
public class __String {
public static void main(String[] args) {
String a = "123";
String d = "123";
String c = new String(d);
System.out.println(a.equals(d)); //equals只要值相同就true,不去考虑地址
System.out.println(c.equals(a));
}
}
3、String常用的方法
public class StringTest1 {
public static void main(String[] args) {
String s1 = "core Java";
String s2 = "Core Java";
System.out.println(s1.charAt(3));//提取下标为3的字符
System.out.println(s2.length());//字符串的长度
System.out.println(s1.equals(s2));//比较两个字符串是否相等
System.out.println(s1.equalsIgnoreCase(s2));//比较两个字符串(忽略大小写)
System.out.println(s1.indexOf("Java"));//字符串s1中是否包含Java
System.out.println(s1.indexOf("apple"));//字符串s1中是否包含apple
String s = s1.replace(' ', '&');//将s1中的空格替换成&
System.out.println("result is :" + s);
String s = "";
String s1 = "How are you?";
System.out.println(s1.startsWith("How"));//是否以How开头
System.out.println(s1.endsWith("you"));//是否以you结尾
s = s1.substring(4);//提取子字符串:从下标为4的开始到字符串结尾为止
System.out.println(s);
s = s1.substring(4, 7);//提取子字符串:下标[4, 7) 不包括7
System.out.println(s);
s = s1.toLowerCase();//转小写
System.out.println(s);
s = s1.toUpperCase();//转大写
System.out.println(s);
String s2 = " How old are you!! ";
s = s2.trim();//去除字符串首尾的空格。注意:中间的空格不能去除
System.out.println(s);
System.out.println(s2);//因为String是不可变字符串,所以s2不变
}
}