working on it ...

Filters

Explore Public Snippets

Sort by

Found 867 snippets matching: constructor

    public by Zenitram.PRO  404470  3  5  1

    Adding an Element in an Array using a Function.

    Arrays of Objects Remember that an object is just another type, like a string or number but more complex. This means that just as we can make arrays of numbers and strings, we can also make arrays of objects. Here we have our Person constructor which should look familiar. We can use this constructor to make an array of Person objects, similar
    // Our person constructor
    function Person (name, age) {
        this.name = name;
        this.age = age;
    }
    
    // Now we can make an array of people
    var family = new Array();
    family[0] = new Person("alice", 40);
    family[1] = new Person("bob", 42);
    family[2] = new Person("michelle", 8);
    // add the last family member, "timmy", who is 6 years old
    family[3] = new Person("timmy", 6);
                                                    

    public by apellizzaro  3388  0  6  1

    Case class constructor

    Best practice on how to have a overloaded constructor for case classes
    Case Class Friend (michele:String, antonio: Int);
    
    Object Friend {
    
    	public Friend (i:Int, s:String) = Friend (s,i)
    }
    
    

    public by lauhin  2392  2  3  0

    Unit test private constructor in java

    Unit test private constructor in java: private_constructor_test.java
    @Test
    public void privateConstructorTest() throws Exception {
        Constructor<MyClass> constructor = MyClass.class.getDeclaredConstructor();
        assertEquals(constructor.isAccessible(), false);
        constructor.setAccessible(true);
        constructor.newInstance((Object[]) null);
    }
    
    

    public by lauhin  2184  1  6  1

    Unit test a private constructor

    use this test method to test a private constructor in java
    import java.lang.reflect.Constructor;
    
    @Test
    public void privateConstructorTest() throws Exception {
        Constructor<MyClass> constructor = MyClass.class.getDeclaredConstructor();
        assertEquals(constructor.isAccessible(), false);
        constructor.setAccessible(true);
        constructor.newInstance((Object[]) null);
    }            

    public by Geometry  1175  0  7  0

    HasDefaultConstructor: Determines whether the given type has a public default (parameterless) constructor.

    Determines whether the given type has a public default (parameterless) constructor. The type to check. True if the type has a public parameterless constructor; false otherwise.
    /// <summary>
    ///   Determines whether the given type has a public default (parameterless) constructor.
    /// </summary>
    /// 
    /// <param name="t">The type to check.</param>
    /// 
    /// <returns>True if the type has a public parameterless constructor; false otherwise.</returns>
    /// 
    public static bool HasDefaultConstructor(this Type t)
    {
        return t.IsValueType || t.GetConstructor(Type.EmptyTypes) != null;
    }

    public by Zenitram.PRO  5046  2  6  1

    Creating new Object using a pre-defined Function(Custom Constructor).

    An Objective Review Let's review the basics of objects covered in our previous lesson on objects. Recall we can create objects using either literal notation or constructor notation. Literal notation creates a single object. Literal notation uses curly brackets { } and the object's default properties are defined within the brackets using p
    var james = {
        // add properties to this object!
        job: "programmer",
        married: false
        
    };
    
    function Person(job, married) {
        this.job = job;
        this.married = married;
    }
    
    // create a "gabby" object using the Person constructor!
    var gabby = new Person("student", true);
                                        

    public by Zenitram.PRO  4148  3  6  1

    Creating new Object Using a pre-defined Function(Custom Constructor) 2.

    Constructors in Review Constructors are a way to make objects with the keyword new. The most basic constructor is the Object constructor, which will make an object with no methods or properties. For more complicated objects we can make our own constructors and put in whatever properties and methods we want. Check out our example to the rig
    function Rabbit(adjective) {
        this.adjective = adjective;
        this.describeMyself = function() {
            console.log("I am a " + this.adjective + " rabbit");
        };
    }
    
    // now we can easily make all of our rabbits
    var rabbit1 = new Rabbit("fluffy");
    var rabbit2 = new Rabbit("happy");
    var rabbit3 = new Rabbit("sleepy"); 
    
    rabbit1.describeMyself();
    rabbit2.describeMyself();
    rabbit3.describeMyself();                                                                                                

    public by Zenitram.PRO  2085  1  5  1

    Customizing Constructor.

    Customizing Constructors In addition to the basic Object constructor, we can define our own custom constructors. These are helpful for two reasons: We can assign our objects properties through parameters we pass in when the object is created. We can give our objects methods automatically. These both work to save us time and lines of
    // 3 lines required to make harry_potter
    var harry_potter = new Object();
    harry_potter.pages = 350;
    harry_potter.author = "J.K. Rowling";
    
    // A custom constructor for book
    function Book (pages, author) {
        this.pages = pages;
        this.author = author;
    }
    
    // Use our new constructor to make the_hobbit in one line
    var the_hobbit = new Book(320,"J.R.R. Tolkien");                        

    public by micurs  12349  4  10  4

    Typescript + underscore: serializing objects

    This little example runs on node.js and assumes you have the typescript type-definition file in the types subdirectory. The example defines two classes one representing a undergrad course in computer science and another one representing possible students for the course. Few things are interesting here: 1 - the overloading of the toString()
    ///<reference path='../types/node.d.ts'/>
    ///<reference path='../types/underscore.d.ts'/>
    
    import _ = require("underscore");
    
    function write(msg) {
        process.stdout.write(msg+'\n');
    }
    
    class Student {
      firstName : string;
      lastName : string;
    
      constructor( n: string, v: string ) {
        this.firstName = n;
        this.lastName = v;
      }
    
      toString() : string {
        return '[ ' + this.firstName + ' ' + this.lastName + ' ]';
      }
    }
    
    class Class {
      title : string = "Computer Science 101";
      participants : Student[] = new Array();
    
      addStudent( s : Student ) : any {
        this.participants.push(s);
      }
    
      toString() : string {
        return this.title + '\n\t' 
              + _.reduce( this.participants, (acc,s) => acc += '\n\t'+ s );
      }
    
    }
    
    var firstSemesterClass : Class = new Class();
    
    firstSemesterClass.addStudent( new Student("John","Smith"));
    firstSemesterClass.addStudent( new Student("Frank","Gehry"));
    
    write( firstSemesterClass );
    
        

    public by mike  1564  1  5  -1

    A singleton implementation in Java

    Java code for singleton design pattern
    public class SingletonClass {
    	static SingletonClass singleton;
    	
    	static { 
    		singleton = new SingletonClass();
    	}
    	
    	private SingletonClass() {
    		//init here...
    	}
    
    	public static SingletonClass get() { 
    		return singleton;
    	}
    }
    • Public Snippets
    • Channels Snippets