Java学习Day06 封装 继承 多态
封装
public class Application {
public static void main ( String [ ] args) {
Student s1 = new Student ( ) ;
s1. setName ( "御狐王" ) ;
System . out. println ( s1. getName ( ) ) ;
s1. setAge ( 100 ) ;
System . out. println ( s1. getAge ( ) ) ;
}
}
public class Student {
private String name;
private int id;
private char gender;
private int age;
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
if ( age > 120 || age < 1 ) {
this . age = 3 ;
} else {
this . age = age;
}
}
public String getName ( ) {
return this . name;
}
public void setName ( String name) {
this . name = name;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public char getGender ( ) {
return gender;
}
public void setGender ( char gender) {
this . gender = gender;
}
}
继承
public class Application {
public static void main ( String [ ] args) {
Student student = new Student ( ) ;
student. say ( ) ;
System . out. println ( student. getMoney ( ) ) ;
}
}
public class Person {
public void say ( ) {
System . out. println ( "say one thing" ) ;
}
private int money = 10_000_000 ;
public int getMoney ( ) {
return money;
}
public void setMoney ( int money) {
this . money = money;
}
}
public class Student extends Person {
}
public class Teacher extends Person {
}
super
public class Application {
public static void main ( String [ ] args) {
Student student = new Student ( ) ;
student. test ( "yyyhuw" ) ;
student. test1 ( ) ;
}
}
public class Person {
protected String name = "御狐王" ;
public Person ( ) {
System . out. println ( "Person无参" ) ;
}
public void print ( ) {
System . out. println ( "Person" ) ;
}
}
public class Student extends Person {
private String name = "yuhuw" ;
public Student ( ) {
super ( ) ;
System . out. println ( "Student无参" ) ;
}
public void print ( ) {
System . out. println ( "Student" ) ;
}
public void test ( String name) {
System . out. println ( name) ;
System . out. println ( this . name) ;
System . out. println ( super . name) ;
}
public void test1 ( ) {
print ( ) ;
this . print ( ) ;
super . print ( ) ;
}
}
方法重写
public class Application {
public static void main ( String [ ] args) {
A a= new A ( ) ;
a. test ( ) ;
B b= new A ( ) ;
b. test ( ) ;
}
}
public class A extends B {
@Override
public void test ( ) {
System . out. println ( "A" ) ;
}
}
public class B {
public void test ( ) {
System . out. println ( "B" ) ;
}
}
多态
public class Application {
public static void main ( String [ ] args) {
Student s1 = new Student ( ) ;
Person s2 = new Student ( ) ;
Object s3 = new Student ( ) ;
s2. run ( ) ;
s1. run ( ) ;
s1. eat ( ) ;
}
}
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) {
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 ) ;
System . out. println ( "=====================" ) ;
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 ) ;
System . out. println ( "=====================" ) ;
Student student = new Student ( ) ;
System . out. println ( student instanceof Student ) ;
System . out. println ( student instanceof Person ) ;
System . out. println ( student instanceof Object ) ;
}
}
public class Person {
public void run ( ) {
System . out. println ( "run" ) ;
}
}
public class Student extends Person {
public void go ( ) {
System . out. println ( "go" ) ;
}
}
public class Teacher extends Person {
}
static
public class Person {
{
System . out. println ( "匿名代码块" ) ;
}
static {
System . out. println ( "静态代码块" ) ;
}
public Person ( ) {
System . out. println ( "构造器" ) ;
}
public static void main ( String [ ] args) {
Person person = new Person ( ) ;
System . out. println ( "===============" ) ;
Person person1 = new Person ( ) ;
}
}
public class Student {
private static int age;
private double score;
public void run ( ) {
}
public static void go ( ) {
}
public static void main ( String [ ] args) {
Student s1 = new Student ( ) ;
System . out. println ( Student . age) ;
System . out. println ( s1. age) ;
System . out. println ( s1. score) ;
}
}
import static java. lang. Math . random ;
public class Test {
public static void main ( String [ ] args) {
System . out. println ( random ( ) ) ;
}
}