Java面向对象高级2
1.代码块
2.内部类
成员内部类
public class Demo{
public static void main(String[] args) {
outer.inner in=new outer().new inner();
in.run();
}
}
class outer{
private String str="outer";
public class inner{
public void run(){
String s=str;
System.out.println(s);
}
}
}
静态内部类
public class Demo{
public static void main(String[] args) {
outer.inner in=new outer.inner();
in.run();
}
}
class outer{
private static String str="outer";
private String str2="outer";
public static class inner{
public void run(){
String s=str;
// String ss=str2; 静态内部类属于外部类无法访问外部成员变量,外部的成员变量属于对象,而不是属于外部类,
//所以可以访问外部类的str,但无法访问属于对象的str2
System.out.println(s);
}
}
}
局部内部类
3.匿名内部类
//普通写法
public class Demo{
public static void main(String[] args) {
Animal c=new cat();
c.cry();
}
}
abstract class Animal{
public abstract void cry();
}
class cat extends Animal{
public void cry(){
System.out.println("猫叫");
}
}
//匿名类写法
public class Demo{
public static void main(String[] args) {
//匿名内部类实际上是有名字的:外部类名$编号.class
//匿名内部类本质是一个子类,同时会立即创建一个子类对象。
Animal c=new Animal() {
@Override
public void cry() {
System.out.println("猫叫");
}
};
c.cry();
}
}
abstract class Animal{
public abstract void cry();
}
//匿名类的反编译文件,该文件的名字为Demo$1.class
class Demo$1 extends Animal {
Demo$1() {
}
public void cry() {
System.out.println("猫叫");
}
}
匿名内部类的常见使用形式
//非匿名类写法
public class Demo{
public static void main(String[] args) {
swimming s=new Student();
doswim(s);
swimming t=new Teacher();
doswim(t);
}
public static void doswim(swimming s){
s.swim();
}
}
class Student implements swimming{
public void swim(){
System.out.println("学生游泳");
}
}
class Teacher implements swimming{
public void swim(){
System.out.println("老师游泳");
}
}
interface swimming{
void swim();
}
//匿名类写法
public class Demo{
public static void main(String[] args) {
swimming s=new swimming() {
@Override
public void swim() {
System.out.println("学生游泳");
}
};
doswim(s);
doswim(new swimming() {
@Override
public void swim() {
System.out.println("老师游泳");
}
});
}
public static void doswim(swimming s){
s.swim();
}
}
interface swimming{
void swim();
}
感觉就是有时候简化代码用的?
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
public class Demo {
public static void main(String[] args) {
student[] s=new student[3];
s[0]=new student("li hua",18);
s[1]=new student("jack",16);
s[2]=new student("red",21);
Arrays.sort(s, new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
return o1.getAge()- o2.getAge();
}
});
for(int i=0;i<s.length;i++){
System.out.println(s[i].getAge());
}
}
}
class student{
private String name;
private int age;
public student(){}
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4.函数式编程-Lambda
public class Demo{
public static void main(String[] args) {
swim s=()->{
System.out.println("学生游泳");
};
s.swimming();
}
}
@FunctionalInterface
interface swim{
void swimming();
}
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
public class Demo {
public static void main(String[] args) {
student[] s=new student[3];
s[0]=new student("li hua",18);
s[1]=new student("jack",16);
s[2]=new student("red",21);
// Arrays.sort(s, new Comparator<student>() {
// @Override
// public int compare(student o1, student o2) {
// return o1.getAge()- o2.getAge();
// }
// });
Arrays.sort(s,(student o1, student o2)->{
return o1.getAge()- o2.getAge();
});
for(int i=0;i<s.length;i++){
System.out.println(s[i].getAge());
}
}
}
class student{
private String name;
private int age;
public student(){}
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
public class Demo {
public static void main(String[] args) {
student[] s=new student[3];
s[0]=new student("li hua",18);
s[1]=new student("jack",16);
s[2]=new student("red",21);
// Arrays.sort(s, new Comparator<student>() {
// @Override
// public int compare(student o1, student o2) {
// return o1.getAge()- o2.getAge();
// }
// });
/* Arrays.sort(s,(student o1, student o2)->{
return o1.getAge()- o2.getAge();
});
*/
Arrays.sort(s,(o1,o2)->o1.getAge()- o2.getAge());
for(int i=0;i<s.length;i++){
System.out.println(s[i].getAge());
}
}
}
class student{
private String name;
private int age;
public student(){}
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
5.函数式编程-方法引用
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
public class Demo {
public static void main(String[] args) {
student[] s=new student[3];
s[0]=new student("li hua",18);
s[1]=new student("jack",16);
s[2]=new student("red",21);
// Arrays.sort(s, new Comparator<student>() {
// @Override
// public int compare(student o1, student o2) {
// return o1.getAge()- o2.getAge();
// }
// });
/* Arrays.sort(s,(student o1, student o2)->{
return o1.getAge()- o2.getAge();
});
*/
// Arrays.sort(s,(o1,o2)->o1.getAge()- o2.getAge());
// Arrays.sort(s,(o1,o2)->student.CompareByAge(o1,o2));
Arrays.sort(s, student::CompareByAge);
for(int i=0;i<s.length;i++){
System.out.println(s[i].getAge());
}
}
}
class student{
private String name;
private int age;
public student(){}
public static int CompareByAge(student o1, student o2){
return o1.getAge()- o2.getAge();
}
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
public class Demo {
public static void main(String[] args) {
student[] s=new student[3];
s[0]=new student("li hua",18);
s[1]=new student("jack",16);
s[2]=new student("red",21);
// Arrays.sort(s, new Comparator<student>() {
// @Override
// public int compare(student o1, student o2) {
// return o1.getAge()- o2.getAge();
// }
// });
/* Arrays.sort(s,(student o1, student o2)->{
return o1.getAge()- o2.getAge();
});
*/
// Arrays.sort(s,(o1,o2)->o1.getAge()- o2.getAge());
// Arrays.sort(s,(o1,o2)->student.CompareByAge(o1,o2));
// Arrays.sort(s, student::CompareByAge);
student t=new student();
Arrays.sort(s, t::Compare);
for(int i=0;i<s.length;i++){
System.out.println(s[i].getAge());
}
}
}
class student{
private String name;
private int age;
public student(){}
public int Compare(student o1,student o2){
return o1.getAge()-o2.getAge();
}
public static int CompareByAge(student o1, student o2){
return o1.getAge()- o2.getAge();
}
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
原文地址:https://blog.csdn.net/m0_53203911/article/details/143765785
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!