(JAVA)泛型

package cn.cast.collection;import java.util.ArrayList;
import java.util.Iterator;/*** @author zhangyu* @date 2021年08月29日 7:43 下午* 泛型:jdk中的泛型为伪泛型* 在编译时期有效,解决安安全问题*/
public class test {public static void main(String[] args) {ArrayList<String> arr = new ArrayList();arr.add("123kahfkadfhkdh");arr.add("234");Iterator<String> it = arr.iterator();while (it.hasNext()){System.out.println(it.next().length());}}}
/eg
public abstract class Company {private String name;private int age;private double salary;public abstract void job();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;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Company(String name, int age, double salary) {this.name = name;this.age = age;this.salary = salary;}
}
/继承
public class Maniger extends Company {private double bonus;public Maniger(String name, int age, double salary, double bouns) {super(name, age, salary);this.bonus = bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}public void job() {System.out.println(getName()+getAge()+getSalary()+getBonus()+"经理");}
}
public class Employee extends Company{public void job() {System.out.println(getName()+getAge()+getSalary()+"程序员");}public Employee(String name, int age, double salary) {super(name, age, salary);}
}
package cn.cast.collection;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;/*** @author zhangyu* @date 2021年09月04日 6:10 下午* f泛型限定:* 1.上限限定 是父类 ? extend E* 2.下限限定 是子类 ? super E*/
public class GenericDome {public static void main(String[] args) {//创建一个集合,存储Manerger对象ArrayList<Maniger> maniger = new ArrayList<>();maniger.add(new Maniger("李四",18,1234.5,456));maniger.add(new Maniger("刘能",23,9807,769));ArrayList<Employee> employee = new ArrayList<>();employee.add(new Employee("谢大脚",12,345));employee.add(new Employee("王长贵",45,809));methon(maniger);methon(employee);}//定义方法,同时遍历两个对象public static void methon(List<? extends Company> list){Iterator<? extends Company> it = list.iterator();while (it.hasNext()){Company c = it.next();c.job();}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421774.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(JAVA)List

package cn.cast.collection;import cn.book.objectarr.Student;import java.util.ArrayList; import java.util.Iterator;/*** author zhangyu* date 2021年08月29日 4:25 下午* 去掉ArrayList 集合中重复元素* List 允许重复* 1.底层可变数组* 2.默认10个位置* 3.每次增加505…

【零基础学习iOS开发】【02-C语言】05-进制

上一讲简单介绍了常量和变量&#xff0c;这讲补充一点计算机的基础知识---进制。 我们先来看看平时是如何表示一个整数的&#xff0c;最常见的肯定是用阿拉伯数字表示&#xff0c;比如“十二”&#xff0c;我们可以用12来表示&#xff0c;其实这种表示方式是基于一种叫做“十进…

(JAVA)可变参数

package cn.cast.collection;/*** author zhangyu* date 2021年09月13日 11:52 下午* 1.可变参数* 2.参数类型指定&#xff0c;参数各个数不确定* 3.修饰符 返回值类型 方法名&#xff08;数据类型...变量名&#xff09;{}* 4.本质是数组* 5.注意事项&#xff1a;* 可变参…

prictice

package cn.cast.collection;import java.util.*;/*** author zhangyu* date 2021年08月29日 7:43 下午* 泛型&#xff1a;jdk中的泛型为伪泛型* 在编译时期有效&#xff0c;解决安安全问题*/ public class test {public static void main(String[] args) { // ArrayLis…

NetworkOnMNetworkOnMainThreadException

这个异常大概意思是在主线程访问网络时出的异常。 Android在4.0之前的版本 支持在主线程中访问网络&#xff0c;但是在4.0以后对这部分程序进行了优化&#xff0c;也就是说访问网络的代码不能写在主线程中了。 用多线程可解决&#xff1a; 1 new Thread(){ 2 …

实现注册登录

package com.logein;/*** author alina* date 2021年09月15日 10:49 下午* User类&#xff0c;封装用户信息的类*/ public class User {//保存用户名private String username;//保存用户密码private String passwoer;public User() { }public User(String username,String pass…

字符串循环同构的最小表示法(转)

循环字符串的最小表示法的问题可以这样描述&#xff1a; 对于一个字符串S&#xff0c;求S的循环的同构字符串S’中字典序最小的一个。 由于语言能力有限&#xff0c;还是用实际例子来解释比较容易&#xff1a;设Sbcad&#xff0c;且S’是S的循环同构的串。S’可以是bcad或者cad…

(JAVA)set

package cn.cast.collection;import java.util.HashSet; import java.util.Iterator; import java.util.Set;/*** author Alina* date 2021年09月16日 11:08 下午* Set 接口派系特点* 不允许存储重复元素* 无序集合&#xff0c;不保证迭代顺序* 没有索引** Set集合迭代方式…

EM Algorithm

(From yesterday to now, I have learning EM for 4 times, and each time I will gain some new ideas and correct some misunderstandings. So cool!) Finally knows that the P(x(i),z(i);θ) means the joint distribution of x and z, parametered by θ. Only if the ta…

(JAVA)hashcode

package cn.cast.collection;/*** author Alina* date 2021年09月19日 8:00 下午* 对象的哈希值* 1.JAVA中&#xff0c;每一个类&#xff0c;都有一个十进制数的哈希值* 2.十进制数&#xff0c;叫做这个对象的哈希值* 3.class Object(){* public native int hashCode();* …

(JAVA)红黑树之自然顺序排序和自定义排序方式

package cn.book.objectarr;/*** author alina* date 2021年08月22日 6:57 下午*/ public class Student implements Comparable<Student> {private String name;private int age;public Student(){}/***** author Alina* date 2021/9/21 9:41 下午* param o* return int…

(JAVA)TreeSet

package homework;/*** author Alina* date 2021年09月22日 10:34 下午*/ class SetTestStudent implements Comparable<SetTestStudent>{private String name;private int gradeScoresOfChinese;private int gradeScoresOfMath;private int gradeScoresOfEnglish;public…

开始

纠结的考试终于结束了。。。可以静下心来整理博客了。。。转载于:https://www.cnblogs.com/Happyhe/archive/2013/05/29/3107121.html

(JAVA)Map集合

package map.demo;import java.util.*;/*** author Alina* date 2021年09月25日 11:20 下午* 底层原理是哈希表&#xff0c;保证唯一性* 允许存储null键&#xff0c;null值* 线程不安全&#xff0c;运行速度快* keyset()可以获取到键* Collections 类的方法reverseOrder :调用空…