Sleepy Generics

master
Caleb Fontenot 2023-10-02 13:28:33 +07:00
parent 1e9d90d12a
commit 7da3542ede
8 changed files with 203 additions and 10 deletions

@ -13,6 +13,7 @@ You can copy and paste the single properties, into the pom.xml file and the IDE
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>JDK_20__System_</netbeans.hint.jdkPlatform>
<netbeans.hint.jdkPlatform>Graal_JDK_20</netbeans.hint.jdkPlatform>
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
</properties>
</project-shared-configuration>

@ -34,11 +34,11 @@ public class GenericRule7 {
}
}
class A {
}
//class A {
//}
class B extends A {
}
//class B extends A {
//}
class C extends B {
}
//class C extends B {
//}

@ -23,13 +23,13 @@ public class GenericRule8<T1> {
public static void main(String[] args)
{
list<A> list1 = new ArrayList<>();
ArrayList<A> list1 = new ArrayList<>();
list1.add(new C());
list1.add(new B());
// list1.add(new B());
f8(list1);
ArrayList<B> list2 = new ArrayList<>();
list2.add(new A());
// list2.add(new A());
f8(list2);
ArrayList<String> list3 = new ArrayList<String>();

@ -0,0 +1,30 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.gemericrules;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author caleb
*/
public class GenericRules10 {
public static void f10(List<? extends A> list1, List<? super C> list2) {
System.out.println(list1.size());
//list1.add(new B());
}
public static void main(String[] args)
{
ArrayList<A> list1 = new ArrayList<A>();
list1.add(new A());
System.out.println(list1.get(0));
ArrayList<Object> list2 = new ArrayList<>();
f10(list1, list2);
ArrayList<Integer> list3 = new ArrayList();
}
}

@ -0,0 +1,96 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.gemericrules;
/**
*
* @author caleb
*/
public class GenericRules10b {
public static <T> T f10b(A<? extends T>[] ar)
{
A<? extends T> temp = ar[0];
ar[0] = ar[1];
ar[1] = temp;
for (int i = 0; i < ar.length; ++i) {
System.out.println(ar[i]);
return ar[i].getT();
}
}
public static void main(String[] args)
{
A<Integer>[] ar = new A[3];
ar[0] = new A(1);
ar[1] = new A(2);
ar[2] = new A(3);
Integer i = f10b(ar);
System.out.println("--------");
B<String>[] ar1 = new B[2];
ar1[0] = new B("John", 45.6); ar1[1] = new B("Mary", new A<Integer>(888));
Object o = f10b(ar1); System.out.println("o");
System.out.println("------------");
}
}
class A<T> {
private T t;
public A()
{
}
public T getT()
{
return t;
}
public void setT(T t)
{
this.t = t;
}
void methodThatTakesParameterT(T t)
{
System.out.println("method p");
}
@Override
public String toString()
{
return "A{" + "t=" + t + "}";
}
}
class B<E> extends A {
private E e;
public B(E e, Object t)
{
super(t);
this.e = e;
}
public E getE()
{
return e;
}
public void setE(E e)
{
this.e = e;
}
@Override
public String toString()
{
return "B{" + "e=" + e + '}';
}
}

@ -0,0 +1,30 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.gemericrules;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author caleb
*/
public class GenericsRule9 {
public static void f9(List<? super C> list) {
System.out.println(list.size());
}
public static void main(String[] args)
{
ArrayList<A> list1 = new ArrayList<>();
list1.add(new A());
f9(list1);
System.out.println(list1.get(0));
ArrayList<Object> list2 = new ArrayList();
f9(list2);
ArrayList<Integer> list3 = new ArrayList();
//f9(list3);
}
}

@ -0,0 +1,36 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.gemericrules;
/**
*
* @author caleb
*/
import java.util.ArrayList;
import java.util.List;
public class GenericsRules11 {
public static void f(List<List<Integer>> table)
{
for (int i = 0; i < 2; ++i) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j < 3; ++j) {
row.add(i + j);
table.add(row);
}
}
for (List<Integer> row : table) {
System.out.println(row);
}
}
public static void main(String[] args) {
f( new ArrayList<List<Integer>>() );
//f( new List<ArrayList<Integer>>() );//will not compile //f( new ArrayList<ArrayList<Integer>>() ); //will not compile /f( new List<List<Integer>>() ); //will not compile
}