//super详解
public class Person {
//无参
public Person() {
System.out.println ("Person无参执行了");
}
protected String name = "rubin";
public void print(){
System.out.println ("Person");
}
}
//public class Application {
// public static void main(String[] args) {
// Student student = new Student ();
// student.test ("小彬");
//student.test1 ();
// }
//}
//public class Student extends Person {
// public Student() {
//隐藏代码:调用了父类的无参构造
//有参"name"
// super();//调用父类的构造器,必须要在子类构造器的第一行
// System.out.println ("Student无参执行了");
//}
// public Student(String name) {
// this.name = name;
// }
// private String name = "Bubbles";
// public void print(){
// System.out.println ("Student");
// }
// public void test1(){
// print (); //Student
// this.print (); //Person
// super.print ();
// }
// public void test(String name){
// System.out.println (name);//小彬
// System.out.println (this.name);//Bubbles
// System.out.println (super.name);//rubin
// }
//}
//方法重写
public class fafa {
//静态的方法和非静态的方法区别很大!
//静态方法: //方法的调用只和左边,定义的数据类型有关
//非静态:重写
public static void main(String[] args) {
// A a = new A();
// a.test ();//A
//父类的引用指向了子类
// B b = new A(); //子类重写了父类的方法
// b.test ();//B
}
}
//继承
//public class A extends B{
//Override重写
// @Override//注解:有功能的注释!
// public void test() {
// System.out.println ("A>=test()");
// }
//}
//public class B {
// public void test() {
// System.out.println ("B>=test()");
// }
//}
//多态
public class duotan1 {
public void run(){
System.out.println ("run");
}
}
public class duotan2 extends duotan1 {
@Override
public void run() {
System.out.println ("son");
}
public void eat(){
System.out.println ("eat");
}
}
public static void main(String[] args) {
//可以指向的引用类型就不确定乐了:父类的引用指向子类
//duotan2 能调用的方法都是自己的或者继承父类的!
duotan2 s1 = new duotan2 ();
//duotan1 父类型, 可以指向子类, 但是不能调用子类独有的方法
duotan1 s2 = new duotan2 ();
Object s3 = new duotan2 ();
//对象能执行哪些方法, 主要看对象左边的类型, 和右边关系不大!
//强制转换
((duotan2) s2).eat (); //子类重写了父类的方法,执行子类的方法
s1.eat ();
}
}
//类型之间的转换:基本类型转换 父类高,子类低
//高 低
//student转换成duotan1
student student = new student ();//子类转换为父类,可能丢自己本来的一些方法!
student.go ();
duotan1 duotan1 = student;
// 案例1
// duotan1 obj = new student ();
//强制转换
// ((student) obj).go ();
}
}
//insetanceof
// Object object = new student ();
// System.out.println (object instanceof student);
// System.out.println (object instanceof duotan1 );
// System.out.println (object instanceof Object);
// System.out.println (object instanceof te);
// System.out.println (object instanceof String);
//static
public class staticDemo1 {
private static int all; //静态的变量
private double add; //非静态的变量
public void run(){
}
public static void go(){
}
// m 和 psvm
public static void main(String[] args) {
go ();
// staticDemo1 s1 = new staticDemo1 ();
// System.out.println (staticDemo1.all);
//System.out.println (s1.all);
//System.out.println (s1.add);
}
}
public class staticPien {
{
//初始值
//2
System.out.println ("匿名代码块");
}
//只执行一次
static {
//1
System.out.println ("静态代码块");
}
public staticPien(){
//3
System.out.println ("构造方法");
}
public static void main(String[] args) {
staticPien staticPien = new staticPien ();
System.out.println ("===================");
staticPien staticPien1 = new staticPien ();
}
}
import 数组.Man;
import javax.crypto.spec.PSource;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;//常量
public class staticTest {
public static void main(String[] args) {
System.out.println (Math.random ()); // System.out.println
// (Math.random ());每一次输出都会输出随机数
System.out.println (PI);//使用常量
}
}
//abstract 抽象类:接口(接口可以多继承)
public abstract class cx1 {
//我们先创个类cx,加;会报错,因为他不是抽象类,
// 在public后面加上abstract(抽象类),这样就是抽象类了
public abstract void cx();
}
public abstract class cx2 extends cx1 { //继承
}
//1.不能new这个抽象类,只能靠子类实现它;约束
//2.抽象类中看一些普通的方法
//3.抽象方法必须在抽象类中
//抽象:约束?
//思考题: new 存在构造器吗?
//存在的意义 抽象出来 提高开发效率
//类可以实现接口 implements接口
public class jieko1Impl implements jieko1,Teime {
//5.导入重写方法实现jieko1里面的四大方法
//6.利用接口实现多继承
@Override
public void run1(String name) {
}
@Override
public void run2(String name) {
}
@Override
public void run3(String name) {
}
@Override
public void run4(String name) {
}
@Override
public void you() {
}
}
//interface定义的关键字 3.接口都需要有实现类
//1.创建一个接口
public interface jieko1 {
// 7.常量 public static final
int t = 999;
//2.定义了四个方法
//接口中的所有定义其实都是抽象,public abstract 不用写,他会自动生成
void run1(String name); //增
void run2(String name);//删
void run3(String name);//改
void run4(String name);//查
}
//4.接口都需要有实现类,所有去创建一个实现类jieko1lmpl
//在创建一个接口
public interface Teime {
void you();//方法
}
//8. 抽象的思维~java
//9.总结
/*
作用:
1.约束
2. 定义一些方法,让不同的人实现~ 10
3. public abstract
4. public static final
5.接口不能被实例化~,接口中没有构造方法~ 6. implements可以实现多个接口
7.必须要重写接口中的方法~
*/
//nbu1 + appin 内部类案例1
public class appin {
public static void main(String[] args) {
//3.调用nbu1
nbu1 nbu1 = new nbu1 ();
//4.通过这个外部类来实例化内部类
nbu1.iner iner = nbu1.new iner ();
//iner.iee ();
iner.pet ();
}
}
public class nbu1 {
//1.外部类
private int pu=10;//8.pu=10,这样他有个默认的值
public void yu(){
System.out.println ("这是外部类");
}
//2.内部类
//10.要是在class后加个static关键字,pet里面的pu就用不了,
// 除非在外部类private后加static关键字才行
public class iner{ //public公共的
public void iee(){
System.out.println ("这是一个内部类");
}
//5.内部类可以访问外部类的一些私有变量
//6.写一个方法,获得外部类的私有属性
public void pet(){
System.out.println (pu);//7.输出pu,pu目前没有值,所以给他写个值
}
//9.然后去appin类,输出 iner.pet ();能不能输出pu的10,
// 是不是从内部类输出了外部类私有属性的10
}
}
//案例2
public class nbu2 {
}
//一个java类可以有多个class类,但是只能有一个public class
//1.这里可以写mian方法
class A{
public static void main(String[] args) { //2.这是个标准的类
}
}
//灰色test是变量名 //4.先注释一下
// test test = new test ();//3.实例化
public static void main(String[] args) {
//5.没有名字的初始化类
new test ().ty ();//6.匿名对象使用,不用将实例保存到变量中
//8.new他usr
usr usr = new usr () { //9.写usr方法他会报错,解决方法,重写里面的方法
@Override
public void Hello() { //10.重写方法
//11.实现了接口的类有没有名字这也是匿名内部类
}
};
}
}
class test{//1.创建test类
public void ty(){ //2.创建ty方法
System.out.println ("1");
}
}
//7.假设是个接口
interface usr{
void Hello();
}
案例3
public class ffnbu2 {
//方法也能写
//1.写个me方法
public void me(){
//2.这叫局部内部类,这跟局部变量一样
class inet{
public void in(){ //3.也可以在这写方法
}
}
}
}