标签:指针 就是 复制 设置 value sed err mount date
final表示最终的、不可变的,对于类和方法来说,abstract 和 final 关键字不能同时使用,因为前者是必须要覆盖重写,后者是不能覆盖重写,自相矛盾
(1)可以用来修饰一个类
(2)可以用来修饰一个方法
(3)可以用来修饰一个局部变量
(4)还可以用来修饰一个成员变量
对于引用类型来说,不可改变说的是变量当中的地址值不可改变
类Myclas: public class MyClass { private String name; public MyClass() { } public MyClass(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 执行类: public class MyDemoFinal { public static void main(String[] args) { final int num = 100; //此时num不可改变了 MyClass c1 = new MyClass("chris"); System.out.println(c1.getName()); System.out.println(c1); c1 = new MyClass("joe"); System.out.println(c1.getName()); System.out.println(c1); } }
final MyClass c1 = new MyClass("chris"); c1 = new MyClass("joe"); //!!!!错误写法
c1.setName("joe"); //这种是可行的 对其内容是可以改变
public class MyClass { private final String name; public MyClass() { name = "chris"; } public MyClass(String name) { this.name = name; } public String getName() { return name; } }
外内部类 public class Body { //内部类 public class heart{ public void methodHeart(){ System.out.println("内部类方法"); System.out.println("我叫" + name); // 可以访问外部的成员变量 } } //外部类的成员变量 private String name; public void methodBody(){ System.out.println("外部类方法"); heart heart = new heart(); heart.methodHeart(); } public String getName() { return name; } public void setName(String name) { this.name = name; } } main: public class MyDemoFinal { public static void main(String[] args) { Body body = new Body(); body.methodBody();//通过调用外部类方法使用内部类方法 } }
public class MyDemoFinal { public static void main(String[] args) { Body body = new Body(); body.methodBody();//通过调用外部类方法使用内部类方法 //直接访问成员内部类 Body.heart directUse = new Body().new heart(); directUse.methodHeart(); } }
public class Outer { int num = 10; //外部类成员变量 public class Inner{ int num = 20; //内部类成员变量 public void methodInner(){ int num = 30; //内部类方法的局部变量 System.out.println(num); //输出的是局部变量 System.out.println(this.num); //输出的是内部类成员变量 System.out.println(Outer.this.num);//外部类的成员变量 } } }
public class Outer { public void methodOuter(){ class Inner { //这是个局部内部类 int num = 10; public void methodInner(){ System.out.println(num); } } Inner in = new Inner(); in.methodInner(); } }
情况:
public class Outer { public void methodOuter(){ int num = 10; class Inner { //这是个局部内部类 public void methodInner(){ System.out.println(num); } } } }
匿名内部类的匿名对象用法 new MyInterface() { @Override public void methodAbs1() { System.out.println("1111"); } @Override public void methodAbs2() { System.out.println("2222"); } }.methodAbs1();
public class DemoOtherClass { public static void main(String[] args) { Person p = new Person("chris",21); String s = p.toString(); System.out.println(s); System.out.println(p); } }
public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; }
public boolean equals(Object obj) { return (this == obj); }
public class DemoOtherClass { public static void main(String[] args) { Person p1 = new Person("chris",21); Person p2 = new Person("joe",20); boolean result = p1.equals(p2); System.out.println(result); System.out.println(p1.equals(p2)); } }
@Override public boolean equals(Object obj){ //增加一个判断,如果传过来的参数obj是this本身,直接返回true if(obj == this){ return true; } //增加一个判断,如果传过来的参数为null,直接返回false,提高程序的效率 if (obj == null){ return false; } //先增加一个判断,判断obj是否是person类型才能转换,比如random,可以防止ClassCastException if (obj instanceof Person){ //使用向下转型 Person p = (Person) obj; //比较两个对象的属性;一个是调用方法的this(p1),一个是p(obj=p2) 此时在使用字符串方法中的equals方法来比较 boolean b = this.name.equals(p.name) && this.age == p.age; return b; } //不是Person类型,就直接返回false return false; }
public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); }
public class DemoOtherClass { public static void main(String[] args) { System.out.println(System.currentTimeMillis());//获取当前系统时间到时间原点经历了多少毫秒 methodDate(); } public static void methodDate(){ Date date = new Date(); System.out.println(date); } }
public static void methodDate(){ Date date1 = new Date(); System.out.println(date1); Date date2 = new Date(0L); System.out.println(date2); }
public class CalendarPratise { public static void main(String[] args) { Date data = new Date(); System.out.println(data.toLocaleString()); //当前电脑下的时间 } }
private static void demo01() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); Date data = new Date(); String timeText = sdf.format(data); System.out.println(data); System.out.println(timeText); //输出的是按照构造方法中指定的模式的字符串 }
private static void demo01() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); Date date = sdf.parse("2020-04-16 13-28-08"); System.out.println(date); }
public class DatePratise { public static void main(String[] args) throws ParseException { Scanner s = new Scanner(System.in); System.out.println("请输入您的生日,格式为yyyy-MM-dd"); String birthday = s.next(); //将获取的生日解析为Date格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dateBirthday = sdf.parse(birthday); //将Date转换为毫秒值 long seconds = dateBirthday.getTime(); //获取当前的日期转换为毫秒值 Long timeNow = new Date().getTime(); //两个时间相减 now - past long time = timeNow - seconds; //将差值转换为天 (s/1000/60/60/24) System.out.println("你已经出生了" + time/1000/60/60/24 +"天"); } }
Calendar calendar = Calendar.getInstance(); //多态
public class CalendarPratise { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //多态 int year = calendar.get(Calendar.YEAR); System.out.println(year); int month = calendar.get(Calendar.MONTH); System.out.println(month); //西方的月份为0-11月 所以我们要加一个月 int date = calendar.get(Calendar.DATE); System.out.println(date); } }
public class CalendarPratise { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //多态 calendar.set(Calendar.YEAR,2050); calendar.set(2012,3,4); //重载方法同时对三个值进行设置 } }
public class CalendarPratise { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //多态 calendar.add(Calendar.YEAR,2);//将年份增加两年 } }
(4)public Date getTime():返回一个表示此Calendar时间值(从历元到现在的毫秒偏移量)的Date对象
把日历对象转为日期对象
public class CalendarPratise { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //多态 Date time = calendar.getTime(); System.out.println(time); } }
Java之路 - final、内部类、Object、Date、Calendar
标签:指针 就是 复制 设置 value sed err mount date
原文地址:https://www.cnblogs.com/caixiaowu/p/12696189.html