public class Attention {
//和类一起加载的
public static void a(){
}
//类实例化之后才有的
public void b(){
}
}
public class PassByValue {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
PassByValue.change(a);
System.out.println(a);
}
//返回值为空
public static void change(int a){
a = 10;
}
}
public class PassByReference {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
PassByReference.change(person);
System.out.println(person.name);
}
//person是一个对象,可以改变属性
public static void change(Person person){
person.name = "Allen";
}
}
class Person{
String name;
}
public class Student {
//属性,字段
String name;
int age;
//一个类即使什么都不写,它也会存在一个方法
public Student(){
}
//有参构造:一旦定义了有参构造,无参就必须显示定义
public Student(String name){
this.name = name;
}
/*
1、使用new关键字,必须要有构造器,本质上实在调用它
2、构造器用来初始化值
*/
//方法
public void study(){
System.out.println(this.name + "在学习!");
}
}
public class Application {
//一个项目应该只存在一个main方法
public static void main(String[] args) {
//类:抽象的实例化
//类实例化后会返回一个自己的对象!
//student对象就是一个Student类的具体实例
Student student = new Student();
student.name = "小明";
student.age = 3;
student.study();
System.out.println(student.age);
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
/*
封装的作用:
1、提高程序的安全性,保护数据
2、隐藏代码的实现细节
3、统一接口
4、系统可维护性增加了
*/
student.setName("Allen");
System.out.println(student.getName());
}
}
public class Student {
//名字
private String name;
//学号
private String id;
//性别
private char sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
//父类Person
public class Person {
public void say(){
System.out.println("Hello");
}
}
/*
public class Person {
//父类私有,子类不能用,只能调用get,set
private int money = 10_0000;
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
*/
//子类(派生类)Student
public class Student extends Person{
}
/*
如果像这样写,即为组合
public class Student{
Person person = new Person();
}
*/
//调用子类继承的父类方法
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
}
}
//Person中没有代码,发现person仍然有方法可以调用
public class Application {
public static void main(String[] args) {
Person person = new Person();
person.getClass();
}
}
//ctrl + H可以看到继承树
在Java中,所有的类都默认直接或间接继承Object类。
public class Animal {
public Animal(){
System.out.println("拉布拉多");
}
//private 私有属性无法被继承
protected String name = "拉布拉多";
}
public class Dog extends Animal{
public Dog(){
//隐藏代码:调用了父类的无参构造 super();
//调用父类的构造器,必须要在子类构造器的第一行
super();
System.out.println("旺财");
}
private String name = "旺财";
public void namePrint(String name){
System.out.println(name); //输出传进来的参数
System.out.println(this.name); //“旺财”
System.out.println(super.name); //“拉布拉多”
}
}
public class Application {
public static void main(String[] args) {
Dog dog = new Dog();
//dog.namePrint("汪汪");
}
}
super注意点:
1、super调用父类的构造方法,必须在构造方法的第一个
2、super必须只能出现在子类或者构造方法中
3、super和this不能同时调用构造方法
VS this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继承条件下才能使用
构造方法:
this():本类的构造
super():父类的构造
public class B {
public static void test(){
System.out.println("B->test()");
}
public void testOverride(){
System.out.println("B->test()");
}
}
public class A extends B{
public static void test(){
System.out.println("A->test()");
}
//重写,是方法的重写,与属性无关
@Override //注解:有功能的注释!
public void testOverride() {
System.out.println("A->test()");
}
}
public class Application {
public static void main(String[] args) {
A a = new A();
B b = new A();
//静态方法:方法的调用只和定义的数据类型有关
a.test();
b.test();
//非静态方法:方法重写
a.testOverride();
b.testOverride();
}
}
因为父类的功能,子类不一定需要,或者不一定满足,所以需要重写。
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//可以指向的引用类型就不确定了 父类的引用指向子类
//Student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person父类型可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
s2.run();//子类重写了父类的方法,执行
s1.run();
((Student) s2).eat();//子类独有的方法只能通过强制准换
}
}
public class Application {
public static void main(String[] args) {
Object object = new Student();
//编译看左,运行看右
System.out.println(object instanceof Student);
System.out.println(object instanceof Person);
System.out.println(object instanceof Object);
System.out.println(object instanceof Teacher);
System.out.println(object instanceof String);
Person person = new Student();
System.out.println(person instanceof Student);
System.out.println(person instanceof Person);
System.out.println(person instanceof Object);
System.out.println(person instanceof Teacher);
//X instanceof Y
//能不能编译通过,取决于X和Y是否存在父子关系
//System.out.println(person instanceof String); 编译错误
}
}
public class Application {
public static void main(String[] args) {
//类型之间的转化: 父 子
//子类转换为父类可能丢失自己的一些方法
Person person = new Student(); //高<---低
((Student)person).eat(); //将person这个对象转换成Student类型,即可使用Student类型的方法。
}
}
1、父类的引用指向子类的对象
2、把子类转换为父类,向上转型
3、把父类转换为子类,向下转型,强制转换
4、方便方法的调用,减少重复的代码
public class Student {
private static int age; //静态的变量
private double score; //非静态的变量
//非静态方法 不能用类名.方法名调用
public void run(){
}
//静态方法 可以用类名.方法名调用
public static void go(){
}
public static void main(String[] args) {
Student student = new Student();
System.out.println(Student.age);
System.out.println(student.age);
System.out.println(student.score);
go();
student.run();
}
}
public class Person {
//2、赋初值
{
System.out.println("匿名代码块");
//匿名代码块
}
//1、只执行一次
{
System.out.println("静态代码块");
//静态代码块
}
//3、构造器
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
}
}
//静态导入包
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
System.out.println(random());
}
}
//抽象类
public abstract class Action {
//约束,需要子类实现
//抽象方法,只有方法名字,没有方法的实现
public abstract void doSomething();
//抽象类中可以有非抽象方法
public void Hello(){
}
}
public class A extends Action{
@Override
public void doSomething() {
}
}
//接口的思维 接口都需要实现类
public interface UserService {
//接口定义的属性为常量 public static final
int AGE = 99;
//接口所有定义的方法其实都是抽象的 public abstract
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
public class UserServiceImpl implements UserService{
//实现了接口的类,就需要重写接口中的方法
@Override
public void add(String name) { }
@Override
public void delete(String name) { }
@Override
public void update(String name) { }
@Override
public void query(String name) { }
}
public class Outer {
private int id = 10;
//成员内部类
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性
public void getID(){
System.out.println(id);
}
}
}
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过外部类实例化内部类
Outer.Inner inner = outer.new Inner();
inner.in();
inner.getID();
}
}
public class Outer {
//静态内部类
public static class Inner {
}
}
public class Outer {
public void method(){
//局部内部类
class Inner{
}
}
}
public class Outer {
public static void main(String[] args) {
//匿名内部类
new Apple().run();
new UserService(){
@Override
public void hello() {
}
};
}
}
interface UserService{
void hello();
}
class Apple{
public void run(){
System.out.println("RUN");
}
}
原文:https://www.cnblogs.com/AllenParker/p/Day07.html